1
+ from pyray import *
2
+ import math
3
+ #Initialization
4
+
5
+ screenWidth = 800
6
+ screenHeight = 450
7
+
8
+ virtualScreenWidth = 160
9
+ virtualScreenHeight = 90
10
+
11
+ virtualRatio = screenWidth / virtualScreenWidth
12
+
13
+ init_window (screenWidth , screenHeight , "raylib [core] example - smooth pixel-perfect camera" )
14
+
15
+ worldSpaceCamera = Camera2D ([0 ]) # Game world camera
16
+ worldSpaceCamera .zoom = 1.0
17
+
18
+ screenSpaceCamera = Camera2D ([0 ]) # Smoothing camera
19
+ screenSpaceCamera .zoom = 1.0
20
+
21
+ target = load_render_texture (virtualScreenWidth , virtualScreenHeight ); # This is where we'll draw all our objects.
22
+
23
+ rec01 = Rectangle (70.0 , 35.0 , 20.0 , 20.0 )
24
+ rec02 = Rectangle (90.0 , 55.0 , 30.0 , 10.0 )
25
+ rec03 = Rectangle (80.0 , 65.0 , 15.0 , 25.0 )
26
+
27
+ # The target's height is flipped (in the source Rectangle), due to OpenGL reasons
28
+ sourceRec = Rectangle (0.0 , 0.0 , target .texture .width , - target .texture .height )
29
+ destRec = Rectangle (- virtualRatio , - virtualRatio , screenWidth + (virtualRatio * 2 ), screenHeight + (virtualRatio * 2 ))
30
+
31
+ origin = Vector2 (0.0 , 0.0 )
32
+
33
+ rotation = 0.0
34
+
35
+ cameraX = 0.0
36
+ cameraY = 0.0
37
+
38
+ set_target_fps (60 )
39
+
40
+ # Main game loop
41
+ while not window_should_close (): # Detect window close button or ESC key
42
+
43
+ # Update
44
+
45
+ rotation += 60.0 * get_frame_time (); # Rotate the rectangles, 60 degrees per second
46
+
47
+ # Make the camera move to demonstrate the effect
48
+ cameraX = (math .sin (get_time ())* 50.0 ) - 10.0
49
+ cameraY = math .cos (get_time ())* 30.0
50
+
51
+ # Set the camera's target to the values computed above
52
+ screenSpaceCamera .target = Vector2 (cameraX , cameraY )
53
+
54
+ # Round worldSpace coordinates, keep decimals into screenSpace coordinates
55
+ worldSpaceCamera .target .x = screenSpaceCamera .target .x
56
+ screenSpaceCamera .target .x -= worldSpaceCamera .target .x
57
+ screenSpaceCamera .target .x *= virtualRatio
58
+
59
+ worldSpaceCamera .target .y = screenSpaceCamera .target .y
60
+ screenSpaceCamera .target .y -= worldSpaceCamera .target .y
61
+ screenSpaceCamera .target .y *= virtualRatio
62
+
63
+ begin_texture_mode (target )
64
+ clear_background (RAYWHITE )
65
+
66
+ begin_mode_2d (worldSpaceCamera )
67
+ draw_rectangle_pro (rec01 , origin , rotation , BLACK )
68
+ draw_rectangle_pro (rec02 , origin , - rotation , RED )
69
+ draw_rectangle_pro (rec03 , origin , rotation + 45.0 , BLUE )
70
+ end_mode_2d ()
71
+ end_texture_mode ()
72
+
73
+ begin_drawing ()
74
+ clear_background (RED )
75
+
76
+ begin_mode_2d (screenSpaceCamera )
77
+ draw_texture_pro (target .texture , sourceRec , destRec , origin , 0.0 , WHITE )
78
+ end_mode_2d ()
79
+
80
+ draw_text (text_format ("Screen resolution: %ix%i" , screenWidth , screenHeight ), 10 , 10 , 20 , DARKBLUE )
81
+ draw_text (text_format ("World resolution: %ix%i" , virtualScreenWidth , virtualScreenHeight ), 10 , 40 , 20 , DARKGREEN )
82
+ draw_fps (get_screen_width () - 95 , 10 )
83
+ end_drawing ()
84
+
85
+
86
+ # De-Initialization
87
+
88
+ unload_render_texture (target ) # Unload render texture
89
+
90
+ close_window (); # Close window and OpenGL context
0 commit comments