├── .gitignore ├── 800px-Euler_two_steps.svg.png ├── Euler_method.svg.png ├── Makefile ├── README.md ├── assets ├── fonts │ ├── FiraCode-Regular.ttf │ └── NimbusSanL-Regu.ttf └── gfx │ ├── bullet.png │ ├── container.png │ ├── elephant.png │ ├── elephant.webp │ ├── elephant_s.png │ ├── gepard.png │ ├── icon.png │ ├── logo.png │ ├── tower_crane.png │ ├── tree.png │ └── yacht.png ├── bullet.gif ├── free_fall.png ├── gravity.gif ├── misc ├── gravity.svg └── tiere_geschwindigkeit ├── notation_1.png ├── notation_2.png ├── shot_0a_s.jpg ├── src ├── 0.c ├── 0a.c ├── 1.c ├── 2.c ├── 3.c ├── 3a.c ├── 3a1.c ├── 4.c ├── 4a.c ├── 4b.c ├── 4c.c ├── 5.c ├── 5a.c ├── 5b.c ├── 5c.c ├── 5d.c ├── 6.c ├── Free Fall with velocity and accelaration.py ├── assets │ ├── Tower Crane.blend │ └── Tower Crane.blend1 ├── helper.c ├── helper.h ├── main.c ├── x1.c ├── x2.c ├── x3.c └── x4.c ├── test.jpg └── time1.png /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | \.anjuta/ 3 | 4 | \.kdev4/ 5 | 6 | @eaDir/ 7 | -------------------------------------------------------------------------------- /800px-Euler_two_steps.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/800px-Euler_two_steps.svg.png -------------------------------------------------------------------------------- /Euler_method.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/Euler_method.svg.png -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -Wall -Wextra -mtune=native -no-pie `sdl2-config --cflags` 2 | LDFLAGS = `sdl2-config --libs` -lSDL2_image -lSDL2_ttf -lm 3 | 4 | .SUFFIXES: 5 | .SUFFIXES: .c .o 6 | 7 | srcdir =src/ 8 | TARGETS = 0 0a 1 2 3 3a 3a1 4 4a 4b 4c main 5 5a 5b 5c 5d 6 9 | 10 | .PHONY: all 11 | all: $(TARGETS) 12 | 13 | 0: $(srcdir)helper.c $(srcdir)0.c 14 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 15 | 16 | 0a: $(srcdir)helper.c $(srcdir)0a.c 17 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 18 | 19 | 1: $(srcdir)helper.c $(srcdir)1.c 20 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 21 | 22 | 2: $(srcdir)helper.c $(srcdir)2.c 23 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 24 | 25 | 3: $(srcdir)helper.c $(srcdir)3.c 26 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 27 | 28 | 3a: $(srcdir)helper.c $(srcdir)3a.c 29 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 30 | 31 | 3a1: $(srcdir)helper.c $(srcdir)3a1.c 32 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 33 | 34 | 4: $(srcdir)helper.c $(srcdir)4.c 35 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 36 | 37 | 4a: $(srcdir)helper.c $(srcdir)4a.c 38 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 39 | 40 | main: $(srcdir)helper.c $(srcdir)main.c 41 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 42 | 43 | 4b: $(srcdir)helper.c $(srcdir)4b.c 44 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 45 | 46 | 4c: $(srcdir)helper.c $(srcdir)4c.c 47 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 48 | 49 | 5: $(srcdir)helper.c $(srcdir)5.c 50 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 51 | 52 | 5a: $(srcdir)helper.c $(srcdir)5a.c 53 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 54 | 55 | 5b: $(srcdir)helper.c $(srcdir)5b.c 56 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 57 | 58 | 5c: $(srcdir)helper.c $(srcdir)5c.c 59 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 60 | 61 | 5d: $(srcdir)helper.c $(srcdir)5d.c 62 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 63 | 64 | 6: $(srcdir)helper.c $(srcdir)6.c 65 | $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) 66 | 67 | .PHONY: clean 68 | clean: 69 | @rm $(TARGETS) 2>/dev/null || true 70 | 71 | # ---------------------------------------------------------------------------------- 72 | 73 | # $< die erste Abhängigkeit 74 | # $@ Name des targets 75 | # $+ eine Liste aller Abhängigkeiten 76 | # $^ eine Liste aller Abhängigkeiten, 77 | # wobei allerdings doppelt vorkommende Abhängigkeiten eliminiert wurden. 78 | 79 | # while inotifywait -e close_write ./src/4c.c; do sleep 0.02; make; done 80 | 81 | # while inotifywait -e close_write ./anim.c; do date +%X; make > /dev/null; done 82 | 83 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # This is a C-SDL series about physics. 4 | 5 | 6 | No matter what type of simulation or game one starts, one needs units or World-Units and a World-Coordinate-System to measure distance and apply velocity - if it is the real world or not. I think getting physics right in first place is cruicial for every program with a time loop. 7 | 8 | Getting the Physics right can be pretty hard. 9 | 10 | ## Measurement and Scaling 11 | 12 | Creating Objects in SI-Units. 13 | 14 | ### Scaling 1 15 | 16 | If the program has been compiled and run, it should show a window with a blue rectangle inside. Well, that seems not very exciting and is something every beginner can do right? 17 | 18 | But... 19 | 20 | Scaling 1 shows how to... 21 | 22 | * get display's DPI, shouldn't it be PPI? 23 | * set window Size in SI-Length-Unit centimeters 24 | * set an visual entity's sized in the same unit 25 | 26 | ![pic 0](./test.jpg) 27 | 28 | Take a ruler and measure if the window without title is 13 time 9 centimeters and the box is 2 x 2 cm. 29 | 30 | Hint for users of the English System: 31 | 1 cm = 0.39370079 inches and 1 inch = 2.54 cm. 32 | 33 | `#define CENTI_PER_INCH 2.54` 34 | 35 | Pixels, points, dots - everything the same? 36 | 37 | Actually a Point is a mathematical (abstract) term. A dot is for example something drawn (written) or printed on paper (applied). A pixel has really more meanings. It can be the the smallest rectangle of a picture during coding and runtime and it can be a tiny, projected, illuminated part (any shape) of the emitter to the display - however it is a logical unit which can be applied if emmited. 38 | 39 | ### Scaling 2 40 | 41 | This program applies the knowledge from the first program. 42 | 43 | Scaling 2 shows how to... 44 | * render Textures in a real world map scaling. 45 | * render a scale bar with description. 46 | 47 | ![pic 0a](./shot_0a_s.jpg) 48 | 49 | 50 | ## Motion in 1 Dimension 51 | 52 | Now that we have physical metrics, we can apply motion. 53 | 54 | ![FreeFall](./free_fall.png) 55 | Typical Diagram of a free fall. 56 | 57 | ### Gravity 58 | Gravity 1 shows how to... 59 | * calculating earth's gravity 60 | * calculate falling time 61 | * apply that gravity on an object 62 | * press left mouse button to start falling 63 | * press right mouse button to reset 64 | * This Demo also shows why we need interpolation (prediction) and derivatives. 65 | ![crane](./gravity.gif) 66 | 67 | ### Velocity 68 | 69 | Velocity 1 shows how to... 70 | * apply horizontal velocity 71 | * press mouse button 1 to reset 72 | 73 | ![bullet](./bullet.gif) 74 | 75 | 76 | ## State Integration 77 | 78 | Integrating the Equations of Motion 79 | 80 | The Physics until this level are really simple, not to say simplified. But, before one can move on with coding there is no reason to go in depth in physics since the low level gamestate, this is the state where visible entities appearance is defined. Points should be at the right time at the right spot. 81 | 82 | ### Time and Steps 83 | 84 | In most fields theoretical, practical or applied sciences one have to deal with time. Math and physics are just classic examples. For me Game Development is practical and applied (Entertainment) computer science which has to deal with time too. 85 | 86 | Note: 87 | I am not going into detail here on Hardware- / Linux- / POSIX API-timers, that will be a dedicated tutorial. 88 | 89 | It should be clear that the more accurate and more precise ones gets the timing, the more realistic the simulation can get. 90 | 91 | #### Timers & Time Resolution 92 | 93 | Ticks and all different flavors of time. 94 | 95 | There are a couple of things involved to get the time in our program. 96 | Hardware clock, Operating System, Core-API || e.g. Linux-Kernel-C-API, The C-Standard Library implementation, e.g. GLibC. And maybe another standard likes POSIX may have to be considered. 97 | 98 | The Hardware delivers the time and the Operating System Kernel API delivers functions to query it. Since I use SDL as abstraction layer - it is a multi platform way to query the timers. 99 | 100 | #### Hardware Sync 101 | 102 | FPS ~ VSync - Vertical Refresh Rate 103 | 104 | **Those 60 Hz** 105 | Hertz is a general unit of frequency (of change in state or cycle in a sound wave, alternating current, or other cyclical waveform) of one cycle per second. A rate of change / clock / clock rate. 106 | 107 | Nowadays most display devices are clocked at 60 Hz, I had 120 Hz Displays for a long time, now they are hard to come by - I think more refresh it is better for the eyes. 108 | 109 | The Display swaps the Picture 60 times a second, now matter how often the GPU does. 110 | 111 | **The Vsync** 112 | Vertical Synchronization - it tries to synchronize (cap) the number of GPU refreshes with the display. That doesn't mean the GPU can produce more frames, just less. 113 | 114 | Ancient Greek: σύν (sýn) means together, Ancient Greek: Κρόνος Krónos means the time, contemporaneous. 115 | 116 | **FPS** 117 | Frame per seconds. 118 | 119 | Frames Per Second it is the number of times display_game() is called per second. 120 | 121 | If I want the calculation as fast as possible no delay is demanded as in real time applications, like airbag control in a real car; one wants to eliminate every possible unnecessary calculation (overhead) to have the maximum time to distribute to urgent actions. Time resolution is one factor that makes calculation more precise. 122 | 123 | As soon if we animate something, even a flipbook we need units lower than a second. And if we want a more realistic motion one needs more sophisticated integration methods. 124 | 125 | 15 / 30 / 60 FPS. 126 | 127 | It's said that one needs 15 pictures per second to percept a fluent motion. In early morning cartoons the frame rate was 6-7, I think. That's why one can see a flickering on animations with a few f/s and it was called stop motion. Search the web for "History of animation" if you are interested. 128 | 129 | Computer Gaming Industry formed 2 rules of thumb, so to say standards, which are 30 FPS for Simulation- / Strategy games and 60 f/s for action games. 130 | 131 | However: In my helper I defined the vsync flag at creation: 132 | `Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);` 133 | 134 | And since my logic and rendering is trivial for modern hardware, we won't drop under 60 f/s, except I mess up the integration or have a serious bug like infinite loops. 135 | 136 | #### Timesteps 137 | 138 | Submultiples of a second are: 139 | ```mono 140 | 10^-1 s ds decisecond 0.1 141 | 10^-2 s cs centisecond 0.01 142 | 10^-3 s ms millisecond 0.001 143 | 10^-6 s µs microsecond 0.000001 144 | 10^-9 s ns nanosecond 0.000000001 145 | ``` 146 | 147 | 60 Hz will result in 16.666666667 milliseconds per timeframe, or 16,666 microseconds. A lot time to do stuff. 148 | 149 | ![](time1.png) 150 | 151 | Time to update: 152 | 153 | * User-Events (Input) 154 | * Simulation-Logic (Updating the Low-Level States), AI 155 | * Music/Sound 156 | * Render the images 157 | 158 | And cut: Frameflip -> next step, while ... 159 | 160 | This sequence is handled through the main loop. 161 | The main loop is the heartbeat of every simulation. 162 | 163 | The simulation Speed is the number of times the state gets updated per second, or in other words, the number of times update_logic() is called per second. 164 | 165 | For those that aren't aware of, I implemented "Variable Delta Time" for the timing in the motion examples. 166 | 167 | So what does a Time-Step utilizing a Variable Delta Time look like? 168 | 169 | Every Time-Frame following code is executed: 170 | ```c 171 | float newTime = time_(); 172 | float deltaTime = newTime - currentTime; 173 | currentTime = newTime; 174 | ``` 175 | 176 | `deltaTime` advances the motion. 177 | 178 | `SDL_GetTicks` delivers passed time in milliseconds and for high precision timer query `SDL_GetPerformanceCounter()` and `SDL_GetPerformanceFrequency()` deliver different hardware dependent values. 179 | 180 | I adjusted my timing function to return a time difference (delta time) in `float`. 181 | 182 | ```c 183 | float time_(void) 184 | { 185 | static Uint64 start = 0; 186 | static float frequency = 0; 187 | 188 | if (start==0){ 189 | start = SDL_GetPerformanceCounter(); 190 | frequency= (float)SDL_GetPerformanceFrequency(); 191 | return 0.0f; 192 | } 193 | 194 | Uint64 counter = 0; 195 | counter=SDL_GetPerformanceCounter(); 196 | return ( ( (float)counter - (float)start ) / frequency ); 197 | } 198 | ``` 199 | Typecasts are quickly messing up the code. 200 | 201 | 202 | So what happens in a Frame-Step utilizing a Variable Delta Time look like? 203 | 204 | S is step or state. 205 | 206 | Everything is initialized: 207 | I is Input - let's say it consume 1 ms. 208 | 209 | ```mono 210 | 1 1 1 1 1 1 1 211 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 212 | S0 - | I 213 | ``` 214 | Init: 215 | prev_frame_tick; 216 | curr_frame_tick = GetTickCount(); 217 | 218 | S0: 219 | prev_frame_tick = curr_frame_tick; 220 | curr_frame_tick = GetTickCount(); 221 | 222 | DT: curr_frame_tick - prev_frame_tick 223 | 224 | Then we wait 225 | Input was qeuried nothing happend. 226 | 227 | VSync really means a **fixed loop time**! (Or Fixed Delay or fixed delta time, all the same.) 228 | 229 | Since the GPU idles until the next clock-flip. 230 | 231 | Other Time-Step methods: 232 | 233 | * Fixed Delay or fixed delta time (the same) 234 | * Fixed loop time 235 | * Variable delta time 236 | * Semi-fixed Timestep 237 | 238 | #### Scientific Notation 239 | 240 | A Notation is a distinct (defined) way to note (write) something. Hence our Keyboard is still limited and people do things for reasons. 241 | 242 | Sometimes it is necessary to work with really large or really small numbers. As example, the gravitational constant, a quantity that relates the force two objects exert on each other, is a very small number. Written in standard decimal notation, it is equal to the following: 243 | ![](notation_1.png) 244 | For small numbers like the gravitational constant, it’s pretty inconvenient to have to write out all the zeros. Fortunately, there is something known as scientific notation that can be used to express large or small numbers in a more compact form. Under this system, numbers are written as a value between 0 and 10, the letter “e,” and a number that represents how many 245 | powers of ten are in the value. For example, the gravitational constant could be written more compactly using scientific notation: 246 | ![](notation_2.png) 247 | For numbers with a magnitude of less than one, the number after the letter “e” is negative. 248 | For values with a magnitude greater than one, the number after the letter “e” is positive. 249 | 250 | The mass of Earth’s moon, for instance, is a very large number and would be expressed in scientific notation as 7.3483e+^22 kg. 251 | 252 | An alternative form of scientific notation is to use a “10” raised to the power of the number of zeros in the value. For example, the mass of Earth’s moon could be alternatively expressed as 7.3483×10^22 kg. If the magnitude of the number is less than one, the exponent on the power of ten would be negative as in 6.67×10^-11. 253 | 254 | You will use scientific notation when you incorporate physics into your game programs, for example, when you define constants like the gravitational constant. Most computer languages recognize scientific notation, so you can incorporate it into your game programs. For example, the following snippet of code would be perfectly acceptable in the Java, C, or C# programming languages: 255 | 256 | `double G = 6.67e-11;` 257 | 258 | 259 | ```c 260 | void earth_gravity(void) 261 | { 262 | //calculating the acceleration with which an object is moving towards 263 | //to the center of the earth 264 | float earth_mass = 5.9736e+24; // in kg 265 | float earth_radius = 6.375e+6; // in km 266 | float gc = 6.674e-11; // gravitational constant 267 | g = ( gc * earth_mass ) / ( earth_radius * earth_radius ); 268 | } 269 | ``` 270 | Dealing with long numbers. 271 | 272 | `5.c` shows the time resolution in C and uses scientific notation, too. 273 | 274 | #### Time and Integration 275 | 276 | In `5a.c` to `5d.c` I implemented the functions to query the high resolution timer in different datatypes. I used float, double and long double and hence SDL queries it with an `unsigned long int` 5d works with those. 277 | 278 | High performance timers will not be available on all platforms and `float` and `double` will not have the same calculation speed across different hardware. There are many options and constraints that will effect the proper implementation of a simulation. 279 | 280 | Before I move on to State-Integration I want to emphasize and clarify two things: 281 | 282 | Logic-Updates per second & Image-Updates per second, do not have to be equally often per second. They are two things. 283 | And a time step is crucial for the integration but both a different subjects, too. 284 | 285 | #### State Integration 286 | 287 | State Integration solves another problem. It calculates where an entity is at a certain time. If our timing function is well prepared, one struggles less to implement more sophisticated Integration Methods. 288 | 289 | What is true for timekeeping is true for State Integration, too. The more precise one works, the more realistic and controllable the result will be. 290 | 291 | In the crane examples, the altitude until `4.c` the position of the container was calculated by this: 292 | 293 | `Altitude = distance_m - 0.5 * g * accumulator * accumulator;` 294 | 295 | It answers the question "How high up are you?" by subtracting the current height from the starting height (z0). 296 | 297 | ```mono 298 | h = 1/2 gt² 299 | 1/2 (9.8 m/s²) * (10 sec)² 300 | h = 490 m 301 | ``` 302 | The formula in C-Notation: 303 | `distance = gravity * powf(time,2) * 0.5;` 304 | 305 | In `6.c` I will use the time difference instead, utilizing the high resolution iTime function and see if there is a difference while running the program. 306 | 307 | Starting with probably the most easiest method for integration: Explicit Euler 308 | 309 | The first step in applying various numerical schemes that emanate from Euler method would be to write Newton's equations of motion as two coupled first-order differential equations. 310 | 311 | ...TBD 312 | 313 | Explicit Euler integration updates the velocity and position as: 314 | 315 | ```c 316 | velocity += timestep * acceleration; 317 | position += timestep * velocity; 318 | 319 | v += g * float_dt_in_seconds; 320 | z -= float_dt_in_seconds * v; 321 | ``` 322 | 323 | Calculating the pixel position from real meters: 324 | 325 | ```c 326 | box.data.frac.pos.y = (float)wh-(real_meter_y*z)-box.data.frac.size.y; 327 | ``` 328 | 329 | ![](800px-Euler_two_steps.svg.png) 330 | Two steps of Euler-Integration. 331 | 332 | 333 | ![](Euler_method.svg.png) 334 | Depicting the miss-callculation of the Euler-method. 335 | 336 | #### More Integration Methods: 337 | 338 | Numerical Integration of Newton's Equations: 339 | 340 | We want Finite Difference Methods, since the integration should finish during a time-step. 341 | 342 | [IH / NIH - Syndrom](https://en.wikipedia.org/wiki/Not_invented_here) 343 | 344 | For me it does not matter who invented it, which method is named after whom or who thinks he invented it first or thought about it first. I think it is an ridiculous discussion - the only concurrency I think about are race conditions. In my opinion a method should be named after what it does and not after a person. 345 | 346 | ...TBD 347 | Nearest neighbor approach 348 | Interpolation 349 | Extrapolation 350 | Derivates 351 | ... 352 | 353 | One Step Methods 354 | * Explicit Euler 355 | 356 | More Step Methos 357 | * Leapfrog 358 | * Verlet 359 | third order for the position and second-order for the velocity. 360 | In the numerical analysis literature, the Verlet method is also knows as the ``explicit central difference method''. 361 | 362 | * Runge and Kutta 363 | 364 | 365 | Other Integration methods: 366 | 367 | Euler-Cromer Method 368 | stable for oscillatory systems 369 | Midpoint and Half-Step Methods 370 | midpoint method yields second-order accuracy for the position and first-order accuracy for the velocity. 371 | Euler-Richardson Method 372 | 373 | 374 | Levels or order of Integration 375 | 376 | ##More motion examples 377 | Boucing Ball, Spring, leaf... 378 | 379 | ## Notes 380 | 381 | ### Useful links: 382 | 383 | #### Physics-Related 384 | 385 | 386 | 387 | 388 | http://www.physics.udel.edu/~bnikolic/teaching/phys660/numerical_ode/ode.html 389 | 390 | #### C-Related 391 | 392 | [Linux-Kernel-C-API](http://man7.org/) 393 | 394 | [My C-Resources](https://gist.github.com/Acry/554e04bab3a2669a5ba2ecd4d673e875) 395 | 396 | [My SDL2-C Demos](https://acry.github.io/c.html) 397 | 398 | ##### Floats and Precision 399 | [Multiple-Precision Floating-Point library](https://www.mpfr.org/) 400 | 401 | What Every Computer Scientist Should Know About Floating-Point Arithmetic - David Goldberg (search for it) 402 | `man 3 printf` 403 | 404 | ##### Time 405 | 406 | https://en.wikipedia.org/wiki/Second 407 | 408 | https://en.wikipedia.org/wiki/Discrete_time_and_continuous_time 409 | 410 | https://wiki.libsdl.org/CategoryTimer 411 | 412 | https://www.guyrutenberg.com/2007/09/10/resolution-problems-in-clock/ 413 | 414 | http://en.wikipedia.org/wiki/C_date_and_time_functions 415 | 416 | http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html 417 | 418 | `man 3 clock` 419 | 420 | `man 7 time` 421 | 422 | [Random ASCII – tech blog of Bruce Dawson about floats](https://randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero-to-100-digits-2/) 423 | 424 | #### SDL-Related 425 | [SDL2](https://www.libsdl.org/) [SDL-Discourse](https://discourse.libsdl.org) 426 | 427 | ### Assets: 428 | Tree: JialiangGao - (former site) www.peace-on-earth.org 429 | 430 | Elephant: 431 | 432 | Sport Yacht: 433 | 434 | ### Contact 435 | 436 | [Get in touch on my Coding Page](https://acry.github.io/) 437 | -------------------------------------------------------------------------------- /assets/fonts/FiraCode-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/fonts/FiraCode-Regular.ttf -------------------------------------------------------------------------------- /assets/fonts/NimbusSanL-Regu.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/fonts/NimbusSanL-Regu.ttf -------------------------------------------------------------------------------- /assets/gfx/bullet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/bullet.png -------------------------------------------------------------------------------- /assets/gfx/container.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/container.png -------------------------------------------------------------------------------- /assets/gfx/elephant.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/elephant.png -------------------------------------------------------------------------------- /assets/gfx/elephant.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/elephant.webp -------------------------------------------------------------------------------- /assets/gfx/elephant_s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/elephant_s.png -------------------------------------------------------------------------------- /assets/gfx/gepard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/gepard.png -------------------------------------------------------------------------------- /assets/gfx/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/icon.png -------------------------------------------------------------------------------- /assets/gfx/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/logo.png -------------------------------------------------------------------------------- /assets/gfx/tower_crane.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/tower_crane.png -------------------------------------------------------------------------------- /assets/gfx/tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/tree.png -------------------------------------------------------------------------------- /assets/gfx/yacht.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/assets/gfx/yacht.png -------------------------------------------------------------------------------- /bullet.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/bullet.gif -------------------------------------------------------------------------------- /free_fall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/free_fall.png -------------------------------------------------------------------------------- /gravity.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/gravity.gif -------------------------------------------------------------------------------- /misc/gravity.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 21 | 39 | 41 | 42 | 44 | image/svg+xml 45 | 47 | 48 | 49 | 50 | 51 | 55 | 67 | 79 | F 90 | G 101 | = 112 | Gm 123 | 1 134 | m 145 | 2 156 | r 167 | 2 178 | 183 | 189 | mass 1 200 | mass 2 211 | 216 | Distance 227 | G=gravitational constant 238 | 6.67e-11 N-m²/kg² 249 | The acceleration of an object due to gravitational force can be calculated byequating the gravitational force equation to Newton’s second law. 265 | Gravitational Force 276 | F 287 | G 298 | = 309 | Gm 320 | 1 331 | m 342 | 2 353 | r 364 | 2 375 | 380 | = 391 | m 402 | 1 413 | a 424 | a is the acceleration of the object 435 | The quantity m 1 is on both sides of the equation and cancels out 446 | a 457 | = 468 | Gm 479 | r 490 | 2 501 | 506 | 2 517 | The acceleration of an objectdue to gravitational force is independent of the mass of the object. 533 | 534 | 535 | -------------------------------------------------------------------------------- /misc/tiere_geschwindigkeit: -------------------------------------------------------------------------------- 1 | Gepard Raubtier 90-120 km/h drei Sekunden. 2 | Mexik. Gabelbock Huftier 88 km/h 3 | Springbock Huftier 88 km/h 4 | Thomson-Gazelle Huftier 80-96 km/h 5 | Hirschziegenantilope Huftier 80 km/h 6 | American Quarter Horse Huftier 70-80 km/h 7 | Gnu Huftier 70-80 km/h 8 | Feldhase Hasenartige 70 km/h 9 | Strauß Laufvogel 70km/h 10 | Windhund Raubtier 70 km/h 11 | Greyhound Raubtier 65-70km/h 12 | Afrikanischer Wildhund Raubtier 65-70 km/h 13 | Koyote Raubtier 69 km/h 14 | Känguru Diprotodontia 64 km/h 15 | Elch Huftier 60 km/h 16 | Löwe Raubtier 55-60 km/h 17 | Nashorn Huftier 45-50 km/h 18 | Eisbär Raubtier 40 km/h 19 | Afrikanischer Elefant Rüsseltier 40 km/h 20 | Flusspferd Huftier 30-40 km/h 21 | Schwarzleguan Reptil 34,9 22 | Sandlaufkäfer Käfer 9 km/h 23 | Kakerlake Insekt 5,4 km/h 24 | -------------------------------------------------------------------------------- /notation_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/notation_1.png -------------------------------------------------------------------------------- /notation_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/notation_2.png -------------------------------------------------------------------------------- /shot_0a_s.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/shot_0a_s.jpg -------------------------------------------------------------------------------- /src/0.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * 6 | * Window Size in SI-Units 7 | * 8 | * DEFINED PROGRESS GOALS: 9 | * 10 | * None atm 11 | * 12 | */ 13 | //END DESCRIPTION 14 | 15 | //BEGIN INCLUDES 16 | //system headers 17 | #include 18 | //local headers 19 | #include "helper.h" 20 | //END INCLUDES 21 | 22 | //BEGIN CPP DEFINITIONS 23 | //Some Color Helpers 24 | //R G B 25 | #define RED 255,0,0,255 26 | #define GREEN 0,255,0,255 27 | #define BLUE 0,0,255,255 28 | //All on 29 | #define WHITE 255,255,255,255 30 | //All off 31 | #define BLACK 0,0,0,255 32 | 33 | 34 | //Real Size in SI-Unit centimeters 35 | #define REALSIZE_H 13.0 36 | #define REALSIZE_V 9.0 37 | 38 | //DPI is Dots per Inch - Industry should fix that 39 | #define CENTI_PER_INCH 2.54 40 | //END CPP DEFINITIONS 41 | 42 | //BEGIN DATASTRUCTURES 43 | struct scalar2{ 44 | float x; 45 | float y; 46 | }; 47 | //END DATASTRUCTURES 48 | 49 | //BEGIN GLOBALS 50 | int ww; 51 | int wh; 52 | 53 | struct scalar2 dpi; 54 | SDL_Rect box; 55 | 56 | //BEGIN VISIBLES 57 | //END VISIBLES 58 | 59 | //END GLOBALS 60 | 61 | //BEGIN FUNCTION PROTOTYPES 62 | void query_disp (void); 63 | void assets_in (void); 64 | //END FUNCTION PROTOTYPES 65 | 66 | //END HEAD 67 | 68 | //BEGIN MAIN FUNCTION 69 | int main(int argc, char *argv[]) 70 | { 71 | 72 | (void)argc; 73 | (void)argv; 74 | 75 | //BEGIN INIT 76 | init(); 77 | //BEGIN WINDOW 78 | // SDL_SetWindowPosition(Window,0,0); 79 | // SDL_SetWindowSize(Window,ww,wh); 80 | char title[10]; 81 | query_disp(); 82 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 83 | sprintf(title, "%dx%d cm",(int)REALSIZE_H,(int)REALSIZE_V); 84 | SDL_SetWindowTitle(Window, title); 85 | SDL_ShowWindow(Window); 86 | //END WINDOW 87 | 88 | //giving Box a size in centimeters 89 | box.w = roundf(2.0/CENTI_PER_INCH*dpi.x); 90 | box.h = roundf(2.0/CENTI_PER_INCH*dpi.y); 91 | 92 | box.x = ww/2-box.w/2; 93 | box.y = wh/2-box.h/2; 94 | 95 | SDL_Event event; 96 | int running = 1; 97 | //END INIT 98 | 99 | //BEGIN MAIN LOOP 100 | while(running){ 101 | 102 | //BEGIN EVENT LOOP 103 | while(SDL_PollEvent(&event)){ 104 | if(event.type == SDL_QUIT){ 105 | running =0; 106 | } 107 | if(event.type == SDL_MOUSEMOTION){ 108 | ; 109 | } 110 | if(event.type == SDL_MOUSEBUTTONDOWN){ 111 | if(event.button.button == SDL_BUTTON_RIGHT){ 112 | ; 113 | } 114 | if(event.button.button == SDL_BUTTON_MIDDLE){ 115 | ; 116 | } 117 | if(event.button.button==SDL_BUTTON_LEFT){ 118 | ; 119 | } 120 | } 121 | if(event.type == SDL_KEYDOWN ){ 122 | switch(event.key.keysym.sym ){ 123 | case SDLK_ESCAPE: 124 | running =0; 125 | break; 126 | 127 | case SDLK_r: 128 | case SDLK_BACKSPACE: 129 | break; 130 | 131 | case SDLK_p: 132 | case SDLK_SPACE: 133 | break; 134 | 135 | default: 136 | break; 137 | } 138 | } 139 | } 140 | //END EVENT LOOP 141 | 142 | //BEGIN LOGIC 143 | //END LOGIC 144 | 145 | //BEGIN RENDERING 146 | SDL_SetRenderDrawColor(Renderer, WHITE); 147 | SDL_RenderClear(Renderer); 148 | 149 | SDL_SetRenderDrawColor(Renderer, BLUE); 150 | SDL_RenderFillRect(Renderer, &box); 151 | 152 | SDL_RenderPresent(Renderer); 153 | //END RENDERING 154 | 155 | } 156 | //END MAIN LOOP 157 | exit_(); 158 | return EXIT_SUCCESS; 159 | 160 | } 161 | //END MAIN FUNCTION 162 | 163 | //BEGIN FUNCTIONS 164 | void query_disp (void) 165 | { 166 | int disp; 167 | 168 | disp=SDL_GetWindowDisplayIndex(Window); 169 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 170 | 171 | SDL_Log("hdpi: %f", dpi.x); 172 | ww=roundf(REALSIZE_H/CENTI_PER_INCH*dpi.x); 173 | 174 | SDL_Log("vdpi: %f", dpi.y); 175 | wh=roundf(REALSIZE_V/CENTI_PER_INCH*dpi.y); 176 | SDL_Log("w: %d, h: %d",ww,wh); 177 | SDL_SetWindowSize(Window, ww,wh); 178 | 179 | } 180 | //END FUNCTIONS 181 | -------------------------------------------------------------------------------- /src/0a.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * 6 | * Linear Scaling 7 | * https://en.wikipedia.org/wiki/Linear_scale 8 | * https://en.wikipedia.org/wiki/Scale_(map) 9 | * 10 | * The scale of a map is the ratio of a distance on the map to the corresponding 11 | * distance on the ground. 12 | * 13 | * DEFINED PROGRESS GOALS: 14 | * 15 | * Bring up a scaling bar 16 | * consisting of rects and describe it with ttf 17 | * 18 | * set up a arithmetic scaling 19 | * show the result 20 | * Tree, elephant, yacht 21 | * 22 | * 23 | * The tree has a height of 30 m in real 24 | * in the window he gonna be 15 cm 25 | * that means: 26 | * 1 cm gonna be 2 m 27 | * Scale: 1:200 28 | * 29 | * NEXT: 30 | * - make window resizeable 31 | * - change scale with mousewheel 32 | * 33 | */ 34 | 35 | //END DESCRIPTION 36 | 37 | //BEGIN INCLUDES 38 | //system headers 39 | #include 40 | //local headers 41 | #include "helper.h" 42 | //END INCLUDES 43 | 44 | //BEGIN CPP DEFINITIONS 45 | //Some Color Helpers 46 | //R G B 47 | #define RED 255,0,0,255 48 | #define GREEN 0,255,0,255 49 | #define BLUE 0,0,255,255 50 | #define BG 112,170,216,255 51 | //All on 52 | #define WHITE 255,255,255,255 53 | //All off 54 | #define BLACK 0,0,0,255 55 | 56 | //Real Size in SI-Unit centimeters 57 | #define REALSIZE_H 16.0*2 58 | #define REALSIZE_V 9.0*2 59 | 60 | //DPI is Dots per Inch - Industry should fix that 61 | #define CENTI_PER_INCH 2.54 62 | 63 | #define TREE_HEIGHT 30.0 //metres 64 | 65 | // #define MAX_VIS_HEIGHT 35 //metres 66 | //The window widht gonna depend on MAX_VIS_HEIGHT 67 | //END CPP DEFINITIONS 68 | 69 | //BEGIN DATASTRUCTURES 70 | //This one is not necessary for real code, redundant with vec2. 71 | //just let it here because it is more descriptive since lenght is a skalar 72 | struct scalar2{ 73 | float x; 74 | float y; 75 | }; 76 | 77 | struct vec2{ 78 | float x; 79 | float y; 80 | }; 81 | //to be more presize one could use scalar2 here in, instead of 82 | //vec2 size 83 | struct rect{ 84 | struct vec2 pos; 85 | struct vec2 size; 86 | }; 87 | 88 | struct entity_static{ 89 | SDL_Texture *Texture; 90 | struct rect frac; 91 | SDL_Rect dst; 92 | }; 93 | //END DATASTRUCTURES 94 | 95 | //BEGIN GLOBALS 96 | int ww; 97 | int wh; 98 | int IARH; //Image Aspect Ratio horizontal 99 | int IARW; //Image Aspect Ratio vertical 100 | struct scalar2 dpi; 101 | SDL_Surface *temp_surface = NULL; 102 | //BEGIN VISIBLES 103 | SDL_Rect box; 104 | 105 | TTF_Font *font = NULL; 106 | struct entity_static sb_desc; 107 | 108 | struct entity_static tree; 109 | struct entity_static elephant; 110 | struct entity_static yacht; 111 | 112 | struct entity_static sbr[8]; 113 | //END VISIBLES 114 | 115 | //END GLOBALS 116 | 117 | //BEGIN FUNCTION PROTOTYPES 118 | int gcd (int, int); 119 | void query_disp (void); 120 | void assets_in (void); 121 | void assets_out (void); 122 | //END FUNCTION PROTOTYPES 123 | 124 | //END HEAD 125 | 126 | //BEGIN MAIN FUNCTION 127 | int main(int argc, char *argv[]) 128 | { 129 | 130 | (void)argc; 131 | (void)argv; 132 | 133 | //BEGIN INIT 134 | init(); 135 | //BEGIN WINDOW 136 | char title[10]; 137 | query_disp(); 138 | //BEGIN SCALING 139 | int gcd_; 140 | gcd_= gcd(REALSIZE_H,REALSIZE_V); 141 | IARH=REALSIZE_V/gcd_; 142 | IARW=REALSIZE_H/gcd_; 143 | SDL_Log("Aspect Ratio: %d:%d",IARW,IARH); 144 | //END SCALING 145 | assets_in(); 146 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 147 | sprintf(title, "%dx%d cm",(int)REALSIZE_H,(int)REALSIZE_V); 148 | SDL_SetWindowTitle(Window, title); 149 | SDL_ShowWindow(Window); 150 | //END WINDOW 151 | 152 | SDL_Event event; 153 | int running = 1; 154 | //END INIT 155 | 156 | //BEGIN MAIN LOOP 157 | while(running){ 158 | 159 | //BEGIN EVENT LOOP 160 | while(SDL_PollEvent(&event)){ 161 | if(event.type == SDL_QUIT){ 162 | running =0; 163 | } 164 | if(event.type == SDL_MOUSEMOTION){ 165 | ; 166 | } 167 | if(event.type == SDL_MOUSEBUTTONDOWN){ 168 | if(event.button.button == SDL_BUTTON_RIGHT){ 169 | ; 170 | } 171 | if(event.button.button == SDL_BUTTON_MIDDLE){ 172 | ; 173 | } 174 | if(event.button.button==SDL_BUTTON_LEFT){ 175 | ; 176 | } 177 | } 178 | if(event.type == SDL_KEYDOWN ){ 179 | switch(event.key.keysym.sym ){ 180 | case SDLK_ESCAPE: 181 | running =0; 182 | break; 183 | 184 | case SDLK_r: 185 | case SDLK_BACKSPACE: 186 | break; 187 | 188 | case SDLK_p: 189 | case SDLK_SPACE: 190 | break; 191 | 192 | default: 193 | break; 194 | } 195 | } 196 | } 197 | //END EVENT LOOP 198 | 199 | //BEGIN LOGIC 200 | //END LOGIC 201 | 202 | //BEGIN RENDERING 203 | SDL_SetRenderDrawColor(Renderer, BG); 204 | SDL_RenderClear(Renderer); 205 | 206 | //BEGIN SCALING BAR 207 | for (int i=0; i<8; i++){ 208 | if (i%2){ 209 | SDL_SetRenderDrawColor(Renderer, WHITE); 210 | SDL_RenderFillRect(Renderer, &sbr[i].dst); 211 | SDL_SetRenderDrawColor(Renderer, BLACK); 212 | SDL_RenderDrawRect(Renderer, &sbr[i].dst); 213 | }else{ 214 | SDL_SetRenderDrawColor(Renderer, BLACK); 215 | SDL_RenderFillRect(Renderer, &sbr[i].dst); 216 | } 217 | } 218 | //END SCALING BAR 219 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 220 | 221 | SDL_RenderCopy(Renderer, tree.Texture, NULL, &tree.dst); 222 | //Flip the elephant for the shadows 223 | SDL_RenderCopyEx(Renderer, elephant.Texture, NULL, &elephant.dst, 0, NULL, 1); 224 | //SDL_RenderCopy(Renderer, elephant.Texture, NULL, &elephant.dst); 225 | SDL_RenderCopy(Renderer, yacht.Texture, NULL, &yacht.dst); 226 | SDL_RenderPresent(Renderer); 227 | //END RENDERING 228 | 229 | } 230 | //END MAIN LOOP 231 | exit_(); 232 | return EXIT_SUCCESS; 233 | 234 | } 235 | //END MAIN FUNCTION 236 | 237 | //BEGIN FUNCTIONS 238 | void assets_in(void) 239 | { 240 | //Load need Images, make them textures and init their dest-rects 241 | int w,h; 242 | //BEGIN TREE 243 | temp_surface = IMG_Load("./assets/gfx/tree.png"); 244 | tree.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 245 | SDL_QueryTexture(tree.Texture, NULL, NULL, &w, &h); 246 | /* 247 | * The tree has a height of 30 m in real. 248 | * In the window he gonna be 15 cm. 249 | * that means: 250 | * 1 cm gonna be 2 m 251 | * Scale: 1:200 252 | * 253 | * We know that 1 cm in the window gonna be 200 cm in real, 254 | * so how many cm's are 30 m's? 255 | * First we want to know how many cm's are one meter, 256 | * it 1 cm devided be 2m or * 0.5 257 | * TREE_HEIGHT * 0.5 = 15 cm 258 | * 259 | */ 260 | tree.dst.h = roundf(TREE_HEIGHT*0.5 / CENTI_PER_INCH*dpi.y); 261 | SDL_Log ("Tree height: %d in Pixel's", tree.dst.h); 262 | float w_helper = h/(TREE_HEIGHT*0.5 / CENTI_PER_INCH*dpi.y); 263 | tree.dst.w = w/w_helper; 264 | tree.dst.y = wh-tree.dst.h; 265 | //Let's set the tree 5 meters to the right 266 | tree.dst.x = roundf(5*0.5/CENTI_PER_INCH*dpi.x); 267 | //END TREE 268 | 269 | //BEGIN ELEPHANT 270 | temp_surface = IMG_Load("./assets/gfx/elephant_s.png"); 271 | elephant.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 272 | SDL_QueryTexture(elephant.Texture, NULL, NULL, &w, &h); 273 | 274 | float real_height=3.3; //m 275 | elephant.dst.h = roundf(real_height*0.5 / CENTI_PER_INCH*dpi.y); 276 | w_helper = h/(real_height*0.5 / CENTI_PER_INCH*dpi.y); 277 | elephant.dst.w = w/w_helper; 278 | elephant.dst.x = tree.dst.w; 279 | elephant.dst.y = wh-elephant.dst.h; 280 | //END ELEPHANT 281 | 282 | //BEGIN YACHT 283 | temp_surface = IMG_Load("./assets/gfx/yacht.png"); 284 | yacht.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 285 | SDL_QueryTexture(yacht.Texture, NULL, NULL, &w, &h); 286 | 287 | float real_lenght=21.7; //m 288 | yacht.dst.w = roundf(real_lenght*0.5 / CENTI_PER_INCH*dpi.x); 289 | float h_helper = w/(real_lenght*0.5 / CENTI_PER_INCH*dpi.x); 290 | yacht.dst.h = h/h_helper; 291 | yacht.dst.x = (elephant.dst.w+elephant.dst.x)+elephant.dst.w; 292 | yacht.dst.y = wh-yacht.dst.h; 293 | //END YACHT 294 | 295 | //BEGIN SCALE BAR RECT 296 | sbr[0].dst.w = roundf(0.5 / CENTI_PER_INCH*dpi.x); 297 | sbr[0].dst.h = sbr[0].dst.w/3; 298 | sbr[0].dst.x = sbr[0].dst.w*2; 299 | sbr[0].dst.y = sbr[0].dst.w*4; 300 | 301 | for (int i=1; i<8; i++){ 302 | sbr[i].dst.w=sbr[0].dst.w; 303 | sbr[i].dst.h=sbr[0].dst.h; 304 | sbr[i].dst.y=sbr[0].dst.y; 305 | 306 | sbr[i].dst.x=sbr[0].dst.x + (sbr[0].dst.w*i); 307 | } 308 | //END SCALE BAR RECT 309 | 310 | //BEGIN SB_DESC 311 | font=TTF_OpenFont("./assets/fonts/NimbusSanL-Regu.ttf", 16); 312 | SDL_Color color={BLACK}; 313 | char text[]="Scale: 1: 200 - 1 cm on the picture are 200 cm in real."; 314 | // sprintf(text, "Scale: 1:200"); 315 | temp_surface=TTF_RenderText_Blended(font,text,color); 316 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 317 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 318 | sb_desc.dst.x=sbr[0].dst.x; 319 | sb_desc.dst.y=sbr[0].dst.y-sbr[0].dst.h-sb_desc.dst.h; 320 | //END SB_DESC 321 | } 322 | void assets_out(void) 323 | { 324 | SDL_DestroyTexture(tree.Texture); 325 | SDL_DestroyTexture(elephant.Texture); 326 | SDL_DestroyTexture(yacht.Texture); 327 | TTF_CloseFont(font); 328 | } 329 | void query_disp (void) 330 | { 331 | int disp; 332 | 333 | disp=SDL_GetWindowDisplayIndex(Window); 334 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 335 | 336 | SDL_Log("hdpi: %f", dpi.x); 337 | ww=roundf(REALSIZE_H/CENTI_PER_INCH*dpi.x); 338 | 339 | SDL_Log("vdpi: %f", dpi.y); 340 | wh=roundf(REALSIZE_V/CENTI_PER_INCH*dpi.y); 341 | SDL_Log("w: %d, h: %d",ww,wh); 342 | SDL_SetWindowSize(Window, ww,wh); 343 | 344 | } 345 | 346 | int gcd (int a, int b) 347 | { 348 | 349 | return (b == 0) ? a : gcd (b, a%b); 350 | 351 | } 352 | //END FUNCTIONS 353 | -------------------------------------------------------------------------------- /src/1.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * Velocity 1 6 | * moving a bullet in slow motion horizontal. 7 | * press left mouse button to reset 8 | */ 9 | 10 | //END DESCRIPTION 11 | 12 | //BEGIN INCLUDES 13 | //system headers 14 | #include 15 | //local headers 16 | #include "helper.h" 17 | //END INCLUDES 18 | 19 | //BEGIN CPP DEFINITIONS 20 | //Some Color Helpers 21 | //R G B 22 | #define RED 255,0,0,255 23 | #define GREEN 0,255,0,255 24 | #define BLUE 0,0,255,255 25 | #define BG 112,170,216,255 26 | //All on 27 | #define WHITE 255,255,255,255 28 | //All off 29 | #define BLACK 0,0,0,255 30 | 31 | //Real Size in SI-Unit centimeters 32 | #define REALSIZE_H 20 33 | #define REALSIZE_V 5 34 | 35 | //DPI is Dots per Inch - Industry should fix that 36 | #define CENTI_PER_INCH 2.54 37 | //END CPP DEFINITIONS 38 | 39 | //BEGIN DATASTRUCTURES 40 | struct vec2{ 41 | float x; 42 | float y; 43 | }; 44 | 45 | struct rect{ 46 | struct vec2 pos; 47 | struct vec2 size; 48 | }; 49 | 50 | struct entity_static{ 51 | SDL_Texture *Texture; 52 | struct rect frac; 53 | SDL_Rect dst; 54 | }; 55 | 56 | struct entity_dyn{ 57 | struct entity_static data; 58 | struct vec2 vel; 59 | }; 60 | //END DATASTRUCTURES 61 | 62 | //BEGIN GLOBALS 63 | int ww; 64 | int wh; 65 | 66 | int IARH; //Image Aspect Ratio horizontal 67 | int IARW; //Image Aspect Ratio vertical 68 | 69 | struct vec2 dpi; 70 | SDL_Surface *temp_surface = NULL; 71 | //BEGIN VISIBLES 72 | SDL_Rect box; 73 | 74 | TTF_Font *font = NULL; 75 | struct entity_static sb_desc; 76 | 77 | struct entity_dyn bullet; 78 | //END VISIBLES 79 | 80 | //END GLOBALS 81 | 82 | //BEGIN FUNCTION PROTOTYPES 83 | int gcd (int, int); 84 | void query_disp (void); 85 | void assets_in (void); 86 | void assets_out (void); 87 | float time_ (void); 88 | void rect_round (struct entity_static *s_entity); 89 | //END FUNCTION PROTOTYPES 90 | 91 | //END HEAD 92 | 93 | //BEGIN MAIN FUNCTION 94 | int main(int argc, char *argv[]) 95 | { 96 | 97 | (void)argc; 98 | (void)argv; 99 | 100 | //BEGIN INIT 101 | init(); 102 | //BEGIN WINDOW 103 | char title[10]; 104 | query_disp(); 105 | //BEGIN SCALING 106 | int gcd_; 107 | gcd_= gcd(REALSIZE_H,REALSIZE_V); 108 | IARH=REALSIZE_V/gcd_; 109 | IARW=REALSIZE_H/gcd_; 110 | SDL_Log("Aspect Ratio: %d:%d",IARW,IARH); 111 | //END SCALING 112 | assets_in(); 113 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 114 | sprintf(title, "%dx%d cm",(int)REALSIZE_H,(int)REALSIZE_V); 115 | SDL_SetWindowTitle(Window, title); 116 | SDL_ShowWindow(Window); 117 | //END WINDOW 118 | float currentTime = 0.0f; 119 | float accumulator = 0.0f; 120 | 121 | SDL_Event event; 122 | int running = 1; 123 | //END INIT 124 | 125 | //BEGIN MAIN LOOP 126 | while(running){ 127 | static char set = 1; 128 | //BEGIN EVENT LOOP 129 | while(SDL_PollEvent(&event)){ 130 | if(event.type == SDL_QUIT){ 131 | running =0; 132 | } 133 | if(event.type == SDL_MOUSEMOTION){ 134 | ; 135 | } 136 | if(event.type == SDL_MOUSEBUTTONDOWN){ 137 | if(event.button.button == SDL_BUTTON_RIGHT){ 138 | ; 139 | } 140 | if(event.button.button == SDL_BUTTON_MIDDLE){ 141 | ; 142 | } 143 | if(event.button.button==SDL_BUTTON_LEFT){ 144 | 145 | accumulator=0; 146 | bullet.data.frac.pos.x = -bullet.data.frac.size.x; 147 | set=1; 148 | break; 149 | } 150 | } 151 | if(event.type == SDL_KEYDOWN ){ 152 | switch(event.key.keysym.sym ){ 153 | case SDLK_ESCAPE: 154 | running =0; 155 | break; 156 | 157 | case SDLK_r: 158 | case SDLK_BACKSPACE: 159 | break; 160 | 161 | case SDLK_p: 162 | case SDLK_SPACE: 163 | break; 164 | 165 | default: 166 | break; 167 | } 168 | } 169 | } 170 | //END EVENT LOOP 171 | 172 | //BEGIN LOGIC 173 | float newTime = time_(); 174 | float deltaTime = newTime - currentTime; 175 | currentTime = newTime; 176 | accumulator += deltaTime; 177 | 178 | if (bullet.data.frac.pos.x + bullet.data.frac.size.x <= ww) 179 | bullet.data.frac.pos.x+=bullet.vel.x*deltaTime; 180 | //The result is pretty good. 181 | 182 | if (set){ 183 | if (bullet.data.frac.pos.x + bullet.data.frac.size.x >= ww){ 184 | // SDL_Log("deltaTime: %f", deltaTime); 185 | SDL_Log("accumulator: %f", accumulator); 186 | set=0; 187 | } 188 | } 189 | bullet.data.dst.x=ceilf(bullet.data.frac.pos.x); 190 | //END LOGIC 191 | 192 | //BEGIN RENDERING 193 | SDL_SetRenderDrawColor(Renderer, BG); 194 | SDL_RenderClear(Renderer); 195 | SDL_RenderCopy(Renderer, bullet.data.Texture, NULL, &bullet.data.dst); 196 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 197 | SDL_RenderPresent(Renderer); 198 | //END RENDERING 199 | 200 | } 201 | //END MAIN LOOP 202 | exit_(); 203 | return EXIT_SUCCESS; 204 | 205 | } 206 | //END MAIN FUNCTION 207 | 208 | //BEGIN FUNCTIONS 209 | void assets_in(void) 210 | { 211 | int w,h; 212 | //BEGIN BULLET 213 | temp_surface = IMG_Load("./assets/gfx/bullet.png"); 214 | bullet.data.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 215 | SDL_QueryTexture(bullet.data.Texture, NULL, NULL, &w, &h); 216 | /* 217 | * 1 cm gonna 1 cm 218 | * so the scale is: 1:1 219 | * bullet diameter: 0.785 cm 220 | * Muzzle velocity: 800 m/s (2,625 ft/s) 221 | * 222 | */ 223 | bullet.data.frac.size.y = 0.785 / CENTI_PER_INCH * dpi.y; 224 | float w_helper = h / (0.785 / CENTI_PER_INCH * dpi.y); 225 | bullet.data.frac.size.x = w/w_helper; 226 | bullet.data.frac.pos.y = (float)wh/2 - bullet.data.frac.size.y/2; 227 | bullet.data.frac.pos.x = 0-bullet.data.frac.size.x; 228 | 229 | rect_round(&bullet.data); 230 | 231 | bullet.vel.y = 0; 232 | bullet.vel.x = 80000; //800 m/s | 800*100 = cm/s | 80,000 cm/s 233 | //we got a 20 cm Window 234 | //so if we want to see something we better slow things down 235 | //Let's say the bullet passes the screen in 4 seconds. 236 | //so it moves with 5 cm per second 237 | 238 | //so with the numbers we travel pixels/acutally verticies 239 | //that exist somewhere in the RAM 240 | //again we need to know how many pixels are a cm. 241 | bullet.vel.x = 5.0 * (dpi.x/CENTI_PER_INCH); 242 | SDL_Log("bullet.vel.x: %f", bullet.vel.x); 243 | //END BULLET 244 | 245 | //BEGIN SB_DESC 246 | font=TTF_OpenFont("./assets/fonts/NimbusSanL-Regu.ttf", 16); 247 | SDL_Color color={BLACK}; 248 | char text[]="Desired speed: 5 cm/s"; 249 | temp_surface=TTF_RenderText_Blended(font,text,color); 250 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 251 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 252 | sb_desc.dst.x=200; 253 | sb_desc.dst.y=100; 254 | //END SB_DESC 255 | } 256 | void assets_out(void) 257 | { 258 | SDL_DestroyTexture(bullet.data.Texture); 259 | TTF_CloseFont(font); 260 | } 261 | 262 | void query_disp (void) 263 | { 264 | int disp; 265 | 266 | disp=SDL_GetWindowDisplayIndex(Window); 267 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 268 | 269 | SDL_Log("hdpi: %f", dpi.x); 270 | ww=roundf(REALSIZE_H/CENTI_PER_INCH*dpi.x); 271 | 272 | SDL_Log("vdpi: %f", dpi.y); 273 | wh=roundf(REALSIZE_V/CENTI_PER_INCH*dpi.y); 274 | SDL_Log("w: %d, h: %d",ww,wh); 275 | SDL_SetWindowSize(Window, ww,wh); 276 | 277 | } 278 | 279 | int gcd (int a, int b) 280 | { 281 | 282 | return (b == 0) ? a : gcd (b, a%b); 283 | 284 | } 285 | 286 | float time_(void) 287 | { 288 | 289 | static Uint64 start = 0; 290 | static float frequency = 0; 291 | 292 | if (start==0){ 293 | start = SDL_GetPerformanceCounter(); 294 | frequency= (float)SDL_GetPerformanceFrequency(); 295 | return 0.0f; 296 | } 297 | 298 | Uint64 counter = 0; 299 | counter=SDL_GetPerformanceCounter(); 300 | return (((float)counter - (float)start) /frequency); 301 | } 302 | 303 | void rect_round(struct entity_static *s_entity) 304 | { 305 | s_entity->dst.x=roundf(s_entity->frac.pos.x); 306 | s_entity->dst.y=roundf(s_entity->frac.pos.y); 307 | s_entity->dst.w=roundf(s_entity->frac.size.x) ; 308 | s_entity->dst.h=roundf(s_entity->frac.size.y) ; 309 | } 310 | //END FUNCTIONS 311 | -------------------------------------------------------------------------------- /src/2.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * Gravity 1 6 | * Dropping a bowling ball from a crane, 7 | * applying gravity 8 | * 9 | * press left mouse button to reset 10 | */ 11 | 12 | //END DESCRIPTION 13 | 14 | //BEGIN INCLUDES 15 | //system headers 16 | #include 17 | //local headers 18 | #include "helper.h" 19 | //END INCLUDES 20 | 21 | //BEGIN CPP DEFINITIONS 22 | //Some Color Helpers 23 | //R G B 24 | #define RED 255,0,0,255 25 | #define GREEN 0,255,0,255 26 | #define BLUE 0,0,255,255 27 | #define BG 112,170,216,255 28 | //All on 29 | #define WHITE 255,255,255,255 30 | //All off 31 | #define BLACK 0,0,0,255 32 | 33 | //Real Size in SI-Unit centimeters 34 | #define REALSIZE_H 20 35 | #define REALSIZE_V 5 36 | 37 | //DPI is Dots per Inch - Industry should fix that 38 | #define CENTI_PER_INCH 2.54 39 | //END CPP DEFINITIONS 40 | 41 | //BEGIN DATASTRUCTURES 42 | struct vec2{ 43 | float x; 44 | float y; 45 | }; 46 | 47 | struct rect{ 48 | struct vec2 pos; 49 | struct vec2 size; 50 | }; 51 | 52 | struct entity_static{ 53 | SDL_Texture *Texture; 54 | struct rect frac; 55 | SDL_Rect dst; 56 | }; 57 | 58 | struct entity_dyn{ 59 | struct entity_static data; 60 | struct vec2 vel; 61 | }; 62 | //END DATASTRUCTURES 63 | 64 | //BEGIN GLOBALS 65 | int ww; 66 | int wh; 67 | 68 | int IARH; //Image Aspect Ratio horizontal 69 | int IARW; //Image Aspect Ratio vertical 70 | 71 | float g; 72 | struct vec2 dpi; 73 | SDL_Surface *temp_surface = NULL; 74 | //BEGIN VISIBLES 75 | SDL_Rect box; 76 | 77 | TTF_Font *font = NULL; 78 | struct entity_static sb_desc; 79 | 80 | struct entity_dyn bullet; 81 | //END VISIBLES 82 | 83 | //END GLOBALS 84 | 85 | //BEGIN FUNCTION PROTOTYPES 86 | int gcd (int, int); 87 | void query_disp (void); 88 | void assets_in (void); 89 | void assets_out (void); 90 | float time_ (void); 91 | void rect_round (struct entity_static *s_entity); 92 | void earth_gravity(void); 93 | //END FUNCTION PROTOTYPES 94 | 95 | //END HEAD 96 | 97 | //BEGIN MAIN FUNCTION 98 | int main(int argc, char *argv[]) 99 | { 100 | 101 | (void)argc; 102 | (void)argv; 103 | 104 | //BEGIN INIT 105 | init(); 106 | //BEGIN WINDOW 107 | char title[10]; 108 | query_disp(); 109 | //BEGIN SCALING 110 | int gcd_; 111 | gcd_= gcd(REALSIZE_H,REALSIZE_V); 112 | IARH=REALSIZE_V/gcd_; 113 | IARW=REALSIZE_H/gcd_; 114 | SDL_Log("Aspect Ratio: %d:%d",IARW,IARH); 115 | //END SCALING 116 | assets_in(); 117 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 118 | sprintf(title, "%dx%d cm",(int)REALSIZE_H,(int)REALSIZE_V); 119 | SDL_SetWindowTitle(Window, title); 120 | SDL_ShowWindow(Window); 121 | //END WINDOW 122 | float currentTime = 0.0f; 123 | float accumulator = 0.0f; 124 | 125 | SDL_Event event; 126 | int running = 1; 127 | //END INIT 128 | 129 | //BEGIN MAIN LOOP 130 | while(running){ 131 | static char set = 1; 132 | //BEGIN EVENT LOOP 133 | while(SDL_PollEvent(&event)){ 134 | if(event.type == SDL_QUIT){ 135 | running =0; 136 | } 137 | if(event.type == SDL_MOUSEMOTION){ 138 | ; 139 | } 140 | if(event.type == SDL_MOUSEBUTTONDOWN){ 141 | if(event.button.button == SDL_BUTTON_RIGHT){ 142 | ; 143 | } 144 | if(event.button.button == SDL_BUTTON_MIDDLE){ 145 | ; 146 | } 147 | if(event.button.button==SDL_BUTTON_LEFT){ 148 | 149 | accumulator=0; 150 | bullet.data.frac.pos.x = -bullet.data.frac.size.x; 151 | set=1; 152 | break; 153 | } 154 | } 155 | if(event.type == SDL_KEYDOWN ){ 156 | switch(event.key.keysym.sym ){ 157 | case SDLK_ESCAPE: 158 | running =0; 159 | break; 160 | 161 | case SDLK_r: 162 | case SDLK_BACKSPACE: 163 | break; 164 | 165 | case SDLK_p: 166 | case SDLK_SPACE: 167 | break; 168 | 169 | default: 170 | break; 171 | } 172 | } 173 | } 174 | //END EVENT LOOP 175 | 176 | //BEGIN LOGIC 177 | float newTime = time_(); 178 | float deltaTime = newTime - currentTime; 179 | currentTime = newTime; 180 | accumulator += deltaTime; 181 | 182 | if (bullet.data.frac.pos.x + bullet.data.frac.size.x <= ww) 183 | bullet.data.frac.pos.x+=bullet.vel.x*deltaTime; 184 | //The result is pretty good. 185 | 186 | if (set){ 187 | if (bullet.data.frac.pos.x + bullet.data.frac.size.x >= ww){ 188 | // SDL_Log("deltaTime: %f", deltaTime); 189 | SDL_Log("accumulator: %f", accumulator); 190 | set=0; 191 | } 192 | } 193 | bullet.data.dst.x=ceilf(bullet.data.frac.pos.x); 194 | //END LOGIC 195 | 196 | //BEGIN RENDERING 197 | SDL_SetRenderDrawColor(Renderer, BG); 198 | SDL_RenderClear(Renderer); 199 | SDL_RenderCopy(Renderer, bullet.data.Texture, NULL, &bullet.data.dst); 200 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 201 | SDL_RenderPresent(Renderer); 202 | //END RENDERING 203 | 204 | } 205 | //END MAIN LOOP 206 | exit_(); 207 | return EXIT_SUCCESS; 208 | 209 | } 210 | //END MAIN FUNCTION 211 | 212 | //BEGIN FUNCTIONS 213 | void assets_in(void) 214 | { 215 | int w,h; 216 | //BEGIN BULLET 217 | temp_surface = IMG_Load("./assets/gfx/bullet.png"); 218 | bullet.data.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 219 | SDL_QueryTexture(bullet.data.Texture, NULL, NULL, &w, &h); 220 | /* 221 | * 1 cm gonna 1 cm 222 | * so the scale is: 1:1 223 | * bullet diameter: 0.785 cm 224 | * Muzzle velocity: 800 m/s (2,625 ft/s) 225 | * 226 | */ 227 | bullet.data.frac.size.y = 0.785 / CENTI_PER_INCH * dpi.y; 228 | float w_helper = h / (0.785 / CENTI_PER_INCH * dpi.y); 229 | bullet.data.frac.size.x = w/w_helper; 230 | bullet.data.frac.pos.y = (float)wh/2 - bullet.data.frac.size.y/2; 231 | bullet.data.frac.pos.x = 0-bullet.data.frac.size.x; 232 | 233 | rect_round(&bullet.data); 234 | 235 | bullet.vel.y = 0; 236 | bullet.vel.x = 80000; //800 m/s | 800*100 = cm/s | 80,000 cm/s 237 | //we got a 20 cm Window 238 | //so if we want to see something we better slow things down 239 | //Let's say the bullet passes the screen in 4 seconds. 240 | //so it moves with 5 cm per second 241 | 242 | //so with the numbers we travel pixels/acutally verticies 243 | //that exist somewhere in the RAM 244 | //again we need to know how many pixels are a cm. 245 | bullet.vel.x = 5.0 * (dpi.x/CENTI_PER_INCH); 246 | SDL_Log("bullet.vel.x: %f", bullet.vel.x); 247 | //END BULLET 248 | 249 | //BEGIN SB_DESC 250 | font=TTF_OpenFont("./assets/fonts/NimbusSanL-Regu.ttf", 16); 251 | SDL_Color color={BLACK}; 252 | char text[]="Desired speed: 5 cm/s"; 253 | temp_surface=TTF_RenderText_Blended(font,text,color); 254 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 255 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 256 | sb_desc.dst.x=200; 257 | sb_desc.dst.y=100; 258 | //END SB_DESC 259 | } 260 | void assets_out(void) 261 | { 262 | SDL_DestroyTexture(bullet.data.Texture); 263 | TTF_CloseFont(font); 264 | } 265 | 266 | void query_disp (void) 267 | { 268 | int disp; 269 | 270 | disp=SDL_GetWindowDisplayIndex(Window); 271 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 272 | 273 | SDL_Log("hdpi: %f", dpi.x); 274 | ww=roundf(REALSIZE_H/CENTI_PER_INCH*dpi.x); 275 | 276 | SDL_Log("vdpi: %f", dpi.y); 277 | wh=roundf(REALSIZE_V/CENTI_PER_INCH*dpi.y); 278 | SDL_Log("w: %d, h: %d",ww,wh); 279 | SDL_SetWindowSize(Window, ww,wh); 280 | 281 | } 282 | 283 | int gcd (int a, int b) 284 | { 285 | 286 | return (b == 0) ? a : gcd (b, a%b); 287 | 288 | } 289 | 290 | float time_(void) 291 | { 292 | 293 | static Uint64 start = 0; 294 | static float frequency = 0; 295 | 296 | if (start==0){ 297 | start = SDL_GetPerformanceCounter(); 298 | frequency= (float)SDL_GetPerformanceFrequency(); 299 | return 0.0f; 300 | } 301 | 302 | Uint64 counter = 0; 303 | counter=SDL_GetPerformanceCounter(); 304 | return (((float)counter - (float)start) /frequency); 305 | } 306 | 307 | void rect_round(struct entity_static *s_entity) 308 | { 309 | s_entity->dst.x=roundf(s_entity->frac.pos.x); 310 | s_entity->dst.y=roundf(s_entity->frac.pos.y); 311 | s_entity->dst.w=roundf(s_entity->frac.size.x) ; 312 | s_entity->dst.h=roundf(s_entity->frac.size.y) ; 313 | } 314 | 315 | void earth_gravity(void) 316 | { 317 | //calculating the accelleration with which an object is moving towards 318 | //to the centre of the earth 319 | float earth_mass = 5.9736e+24; //in kg 320 | float earth_radius = 6.375e+6; //in km 321 | float gc = 6.674e-11; //gravitational constant 322 | g=(gc*earth_mass)/(earth_radius*earth_radius); 323 | } 324 | //END FUNCTIONS 325 | -------------------------------------------------------------------------------- /src/3.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * 6 | * Gravity 1 7 | * Calculating Earth's gravity. 8 | * Calculate falling time. 9 | * Dropping a container from a tower crane, 10 | * applying gravity 11 | * 12 | * press left mouse button to start falling 13 | * press right mouse button to reset 14 | * 15 | * This Demo also shows why we need interpoltion (prediction) and derivatives. 16 | * 17 | */ 18 | 19 | //END DESCRIPTION 20 | 21 | //BEGIN INCLUDES 22 | //system headers 23 | #include 24 | //local headers 25 | #include "helper.h" 26 | //END INCLUDES 27 | 28 | //BEGIN CPP DEFINITIONS 29 | //Some Color Helpers 30 | //R G B 31 | #define RED 255,0,0,255 32 | #define GREEN 0,255,0,255 33 | #define BLUE 0,0,255,255 34 | #define BG 112,170,216,255 35 | //All on 36 | #define WHITE 255,255,255,255 37 | //All off 38 | #define BLACK 0,0,0,255 39 | 40 | //Real Size in SI-Unit meters 41 | #define REALSIZE_H 82.1305 42 | #define REALSIZE_V 52.8554 43 | 44 | #define SCALE_RATIO 4.0 45 | 46 | //Real Size in SI-Unit centimeters 47 | #define SCALE_UNITS 100.0 48 | 49 | #define SCALESIZE_H REALSIZE_H/SCALE_RATIO 50 | #define SCALESIZE_V REALSIZE_V/SCALE_RATIO 51 | 52 | //DPI is Dots per Inch - Industry should fix that 53 | #define CENTI_PER_INCH 2.54 54 | //END CPP DEFINITIONS 55 | 56 | //BEGIN DATASTRUCTURES 57 | struct vec2{ 58 | float x; 59 | float y; 60 | }; 61 | 62 | struct rect{ 63 | struct vec2 pos; 64 | struct vec2 size; 65 | }; 66 | 67 | struct entity_static{ 68 | SDL_Texture *Texture; 69 | struct rect frac; 70 | SDL_Rect dst; 71 | }; 72 | 73 | struct entity_dyn{ 74 | struct entity_static data; 75 | struct vec2 vel; 76 | }; 77 | //END DATASTRUCTURES 78 | 79 | //BEGIN GLOBALS 80 | int ww; 81 | int wh; 82 | 83 | int IARH; //Image Aspect Ratio horizontal 84 | int IARW; //Image Aspect Ratio vertical 85 | 86 | float g; 87 | struct vec2 dpi; 88 | 89 | float screen_centimeter_x; 90 | float screen_centimeter_y; 91 | 92 | float real_meter_x; 93 | float real_meter_y; 94 | 95 | float distance_m; 96 | float distance_p; 97 | 98 | SDL_Surface *temp_surface = NULL; 99 | //BEGIN VISIBLES 100 | 101 | 102 | TTF_Font *font = NULL; 103 | struct entity_static sb_desc; 104 | struct entity_static tower_crane; 105 | struct entity_dyn box; 106 | //END VISIBLES 107 | 108 | //END GLOBALS 109 | 110 | //BEGIN FUNCTION PROTOTYPES 111 | void query_disp (void); 112 | void assets_in (void); 113 | void assets_out (void); 114 | float time_ (void); 115 | void rect_round (struct entity_static *s_entity); 116 | void earth_gravity(void); 117 | //END FUNCTION PROTOTYPES 118 | 119 | //END HEAD 120 | 121 | //BEGIN MAIN FUNCTION 122 | int main(int argc, char *argv[]) 123 | { 124 | 125 | (void)argc; 126 | (void)argv; 127 | 128 | //BEGIN INIT 129 | 130 | init(); 131 | query_disp(); 132 | earth_gravity(); 133 | SDL_Log("Gravity: %.2f m/s²", g); 134 | assets_in(); 135 | 136 | //BEGIN WINDOW 137 | 138 | char title[20]; 139 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 140 | sprintf(title, "%.2f x %.2f cm",SCALESIZE_H,SCALESIZE_V); 141 | SDL_SetWindowTitle(Window, title); 142 | SDL_ShowWindow(Window); 143 | 144 | //END WINDOW 145 | float currentTime = 0.0f; 146 | float accumulator = 0.0f; 147 | 148 | SDL_Event event; 149 | int running = 1; 150 | float Altitude=distance_m; 151 | //END INIT 152 | 153 | //BEGIN MAIN LOOP 154 | while(running){ 155 | static char set = 0; 156 | //BEGIN EVENT LOOP 157 | while(SDL_PollEvent(&event)){ 158 | if(event.type == SDL_QUIT){ 159 | running =0; 160 | } 161 | if(event.type == SDL_MOUSEMOTION){ 162 | ; 163 | } 164 | if(event.type == SDL_MOUSEBUTTONDOWN){ 165 | if(event.button.button == SDL_BUTTON_RIGHT){ 166 | box.data.frac.pos.y = (float)wh-(real_meter_y*distance_m)-box.data.frac.size.y; 167 | box.data.dst.y = ceilf(box.data.frac.pos.y); 168 | 169 | } 170 | if(event.button.button == SDL_BUTTON_MIDDLE){ 171 | ; 172 | } 173 | if(event.button.button==SDL_BUTTON_LEFT){ 174 | 175 | accumulator=0; 176 | Altitude=distance_m; 177 | set=1; 178 | break; 179 | } 180 | } 181 | if(event.type == SDL_KEYDOWN ){ 182 | switch(event.key.keysym.sym ){ 183 | case SDLK_ESCAPE: 184 | running =0; 185 | break; 186 | 187 | case SDLK_r: 188 | case SDLK_BACKSPACE: 189 | break; 190 | 191 | case SDLK_p: 192 | case SDLK_SPACE: 193 | break; 194 | 195 | default: 196 | break; 197 | } 198 | } 199 | } 200 | //END EVENT LOOP 201 | 202 | float newTime = time_(); 203 | float deltaTime = newTime - currentTime; 204 | currentTime = newTime; 205 | accumulator += deltaTime; 206 | if (set){ 207 | if (Altitude > 0.0f){ 208 | //Box Altitude dropping over time 209 | Altitude = distance_m - 0.5 * g * accumulator * accumulator; 210 | SDL_Log("Altitude: %f", Altitude); 211 | SDL_Log("accumulator: %f", accumulator); 212 | 213 | box.data.frac.pos.y = (float)wh-(real_meter_y*Altitude)-box.data.frac.size.y; 214 | box.data.dst.y = ceilf(box.data.frac.pos.y); 215 | } 216 | } 217 | 218 | 219 | 220 | 221 | 222 | //BEGIN RENDERING 223 | SDL_SetRenderDrawColor(Renderer, BG); 224 | SDL_RenderClear(Renderer); 225 | SDL_RenderCopy(Renderer, tower_crane.Texture, NULL, &tower_crane.dst); 226 | SDL_RenderCopy(Renderer, box.data.Texture, NULL, &box.data.dst); 227 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 228 | SDL_RenderPresent(Renderer); 229 | //END RENDERING 230 | 231 | } 232 | //END MAIN LOOP 233 | exit_(); 234 | return EXIT_SUCCESS; 235 | 236 | } 237 | //END MAIN FUNCTION 238 | 239 | //BEGIN FUNCTIONS 240 | void assets_in(void) 241 | { 242 | //BEGIN TOWER CRANE 243 | temp_surface = IMG_Load("./assets/gfx/tower_crane.png"); 244 | tower_crane.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 245 | tower_crane.frac.size.y = wh; 246 | tower_crane.frac.size.x = ww; 247 | tower_crane.frac.pos.y = 0; 248 | tower_crane.frac.pos.x = 0; 249 | rect_round(&tower_crane); 250 | //END TOWER_CRANE 251 | 252 | //How many pixels are one centimeter on the screen? 253 | screen_centimeter_x = (SCALESIZE_H * SCALE_RATIO / CENTI_PER_INCH * dpi.x) / REALSIZE_H; 254 | SDL_Log("pixels are one centimeter on the picture x: %f", screen_centimeter_x); 255 | screen_centimeter_y = (SCALESIZE_V * SCALE_RATIO / CENTI_PER_INCH * dpi.y) / REALSIZE_V; 256 | SDL_Log("pixels are one centimeter on the picture y: %f", screen_centimeter_y); 257 | 258 | //How many pixels are one real meter? 259 | float screen_size_x=SCALESIZE_H / CENTI_PER_INCH * dpi.x; //it is ww, just repeated for precision 260 | float screen_size_y=SCALESIZE_V / CENTI_PER_INCH * dpi.y; //it is wh, just repeated for precision 261 | 262 | SDL_Log("screen_size_y: %f", screen_size_y); 263 | 264 | real_meter_x=screen_size_x/REALSIZE_H; 265 | SDL_Log("real_meter_x in pixels: %f", real_meter_x); 266 | 267 | real_meter_y=screen_size_y/REALSIZE_V; 268 | SDL_Log("real_meter_y in pixels: %f", real_meter_y); 269 | 270 | //BEGIN TESTBOX 271 | temp_surface = IMG_Load("./assets/gfx/container.png"); 272 | box.data.Texture=SDL_CreateTextureFromSurface(Renderer, temp_surface); 273 | box.data.frac.size.y = real_meter_x*2.5; 274 | box.data.frac.size.x = real_meter_y*8; 275 | box.data.frac.pos.x = real_meter_x*67.5; 276 | 277 | box.data.frac.pos.y = real_meter_y*12.3; 278 | rect_round(&box.data); 279 | box.vel.y = 0; 280 | box.vel.x = 0; 281 | //END TESTBOX 282 | 283 | //falling distance in real meters 284 | distance_m=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y)/real_meter_y; 285 | SDL_Log("falling distance: %.2f m.", distance_m); 286 | 287 | distance_p=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y); 288 | SDL_Log("falling distance in pixel: %f", distance_p); 289 | 290 | // These are the expected results for our values: 291 | float fall_time = sqrtf(2 * distance_m / g); 292 | SDL_Log("fall time: %f seconds", fall_time); 293 | // t 3,0142 294 | // v 29,5596 m/s 295 | // 106,4147 km/h 296 | 297 | //BEGIN SB_DESC 298 | font=TTF_OpenFont("./assets/fonts/NimbusSanL-Regu.ttf", 16); 299 | SDL_Color color={BLACK}; 300 | char text[30]; 301 | sprintf(text, "Map scale = 1: %.0f cm",SCALE_RATIO*SCALE_UNITS); 302 | temp_surface=TTF_RenderText_Blended(font,text,color); 303 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 304 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 305 | sb_desc.dst.x=ww-sb_desc.dst.w-50; 306 | sb_desc.dst.y=25; 307 | //END SB_DESC 308 | } 309 | 310 | void assets_out(void) 311 | { 312 | SDL_DestroyTexture(tower_crane.Texture); 313 | SDL_DestroyTexture(box.data.Texture); 314 | TTF_CloseFont(font); 315 | } 316 | 317 | void query_disp (void) 318 | { 319 | int disp; 320 | 321 | disp=SDL_GetWindowDisplayIndex(Window); 322 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 323 | SDL_Log("hdpi: %f", dpi.x); 324 | ww=roundf(SCALESIZE_H / CENTI_PER_INCH * dpi.x); 325 | SDL_Log("vdpi: %f", dpi.y); 326 | wh=roundf(SCALESIZE_V / CENTI_PER_INCH * dpi.y); 327 | SDL_Log("w: %d, h: %d",ww,wh); 328 | SDL_SetWindowSize(Window, ww,wh); 329 | 330 | } 331 | 332 | float time_(void) 333 | { 334 | 335 | static Uint64 start = 0; 336 | static float frequency = 0; 337 | 338 | if (start==0){ 339 | start = SDL_GetPerformanceCounter(); 340 | frequency= (float)SDL_GetPerformanceFrequency(); 341 | return 0.0f; 342 | } 343 | 344 | Uint64 counter = 0; 345 | counter=SDL_GetPerformanceCounter(); 346 | return (((float)counter - (float)start) /frequency); 347 | } 348 | 349 | void rect_round(struct entity_static *s_entity) 350 | { 351 | s_entity->dst.x=roundf(s_entity->frac.pos.x); 352 | s_entity->dst.y=roundf(s_entity->frac.pos.y); 353 | s_entity->dst.w=roundf(s_entity->frac.size.x); 354 | s_entity->dst.h=roundf(s_entity->frac.size.y); 355 | } 356 | 357 | void earth_gravity(void) 358 | { 359 | //calculating the accelleration with which an object is moving towards 360 | //to the centre of the earth 361 | float earth_mass = 5.9736e+24; //in kg 362 | float earth_radius = 6.375e+6; //in km 363 | float gc = 6.674e-11; //gravitational constant 364 | g=(gc*earth_mass)/(earth_radius*earth_radius); 365 | } 366 | //END FUNCTIONS 367 | -------------------------------------------------------------------------------- /src/3a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | 8 | float distance = 50; 9 | float gravity = 9.81; 10 | float time = sqrtf(2 * distance / gravity); 11 | printf("t: %f\n",time); 12 | return EXIT_SUCCESS; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/3a1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int main(void) 7 | { 8 | 9 | float distance = 38.06; // metres 10 | float gravity = 9.81; // metres/seconds² 11 | 12 | float time = sqrtf(2 * distance / gravity); 13 | 14 | printf("Gravity is a defined constant, g = -9.8 m/s²\n"); 15 | printf("Given the distance of %.2f m's the falling time is: %.2f seconds.\n", distance ,time); 16 | 17 | // You're standing at the edge of a cliff and drop a ball. It takes 10 sec to hit the ground. 18 | 19 | time = 10.0; 20 | 21 | // How high up are you? 22 | // h = 1/2 gt² 23 | // 1/2 (9.8 m/s²) * (10 sec)² 24 | // h = 490 m 25 | distance = gravity * powf(time,2) * 0.5; 26 | printf("Given the time of %.2f seconds the distance is: %.2f metres.\n",time, distance); 27 | 28 | // What was the velocity of the ball? 29 | // v = gt 30 | // v = 9.8 m/s² * 10 seconds. 31 | // v = 98 m/s 32 | float velocity; 33 | velocity = gravity * time; 34 | printf("Given the time of %.2f seconds the velocity is: %.2f metres/second.\n",time, velocity); 35 | 36 | // 1 Kilometer sind 1000 Meter. 37 | // 1 Stunde sind 3600 Sekunden. 38 | float km_h = velocity/1000*3600; 39 | 40 | printf("That are %.2f kilometres per hour.\n",km_h); 41 | km_h = velocity*3.6; 42 | printf("That are %.2f kilometres per hour.\n",km_h); 43 | 44 | float acceleration; 45 | // a = v/t 46 | acceleration = velocity / time; 47 | printf("Acc: %.2f .\n",acceleration); 48 | return EXIT_SUCCESS; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/4.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * 6 | * Derivatives 1 7 | * 8 | * Due to the constant accelleration caused by gravity, 9 | * the result in Gravity 1 10 | * https://github.com/Acry/SDL2-Physics/blob/master/src/3.c 11 | * is far from precise. 12 | * When the container hits the ground the velocity is so high 13 | * that the point in time of ground impact is missed. 14 | * 15 | * I try to fix that now utilizing derivatives. 16 | * Going to try to interploate the velocity and position in a finer grain. 17 | * 18 | * 19 | * Altitude in meter 20 | * Velocity in m/s 21 | * Accelleration in m/s^2 22 | * 23 | * spring constant, k, 24 | */ 25 | 26 | //END DESCRIPTION 27 | 28 | //BEGIN INCLUDES 29 | //system headers 30 | #include 31 | //local headers 32 | #include "helper.h" 33 | //END INCLUDES 34 | 35 | //BEGIN CPP DEFINITIONS 36 | //Some Color Helpers 37 | //R G B 38 | #define RED 255,0,0,255 39 | #define GREEN 0,255,0,255 40 | #define BLUE 0,0,255,255 41 | #define BG 112,170,216,255 42 | //All on 43 | #define WHITE 255,255,255,255 44 | //All off 45 | #define BLACK 0,0,0,255 46 | 47 | //Real Size in SI-Unit meters 48 | #define REALSIZE_H 82.1305 49 | #define REALSIZE_V 52.8554 50 | 51 | #define SCALE_RATIO 4.0 52 | 53 | //Real Size in SI-Unit centimeters 54 | #define SCALE_UNITS 100.0 55 | 56 | #define SCALESIZE_H REALSIZE_H/SCALE_RATIO 57 | #define SCALESIZE_V REALSIZE_V/SCALE_RATIO 58 | 59 | //DPI is Dots per Inch - Industry should fix that 60 | #define CENTI_PER_INCH 2.54 61 | //END CPP DEFINITIONS 62 | 63 | //BEGIN DATASTRUCTURES 64 | struct vec2{ 65 | float x; 66 | float y; 67 | }; 68 | 69 | struct rect{ 70 | struct vec2 pos; 71 | struct vec2 size; 72 | }; 73 | 74 | struct entity_static{ 75 | SDL_Texture *Texture; 76 | struct rect frac; 77 | SDL_Rect dst; 78 | }; 79 | 80 | struct entity_dyn{ 81 | struct entity_static data; 82 | struct vec2 vel; 83 | }; 84 | //END DATASTRUCTURES 85 | 86 | //BEGIN GLOBALS 87 | int ww; 88 | int wh; 89 | 90 | int IARH; //Image Aspect Ratio horizontal 91 | int IARW; //Image Aspect Ratio vertical 92 | 93 | float g; 94 | struct vec2 dpi; 95 | 96 | float screen_centimeter_x; 97 | float screen_centimeter_y; 98 | 99 | float real_meter_x; 100 | float real_meter_y; 101 | 102 | float distance_m; 103 | float distance_p; 104 | 105 | SDL_Surface *temp_surface = NULL; 106 | //BEGIN VISIBLES 107 | 108 | 109 | TTF_Font *font = NULL; 110 | struct entity_static sb_desc; 111 | struct entity_static tower_crane; 112 | struct entity_dyn box; 113 | //END VISIBLES 114 | 115 | //END GLOBALS 116 | 117 | //BEGIN FUNCTION PROTOTYPES 118 | void query_disp (void); 119 | void assets_in (void); 120 | void assets_out (void); 121 | float time_ (void); 122 | void rect_round (struct entity_static *s_entity); 123 | void earth_gravity(void); 124 | //END FUNCTION PROTOTYPES 125 | 126 | //END HEAD 127 | 128 | //BEGIN MAIN FUNCTION 129 | int main(int argc, char *argv[]) 130 | { 131 | 132 | (void)argc; 133 | (void)argv; 134 | 135 | //BEGIN INIT 136 | 137 | init(); 138 | query_disp(); 139 | earth_gravity(); 140 | SDL_Log("Gravity: %.2f m/s²", g); 141 | assets_in(); 142 | 143 | //BEGIN WINDOW 144 | 145 | char title[20]; 146 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 147 | sprintf(title, "%.2f x %.2f cm",SCALESIZE_H,SCALESIZE_V); 148 | SDL_SetWindowTitle(Window, title); 149 | SDL_ShowWindow(Window); 150 | 151 | //END WINDOW 152 | float currentTime = 0.0f; 153 | float accumulator = 0.0f; 154 | 155 | SDL_Event event; 156 | int running = 1; 157 | float Altitude=distance_m; 158 | //END INIT 159 | 160 | //BEGIN MAIN LOOP 161 | while(running){ 162 | static char set = 0; 163 | //BEGIN EVENT LOOP 164 | while(SDL_PollEvent(&event)){ 165 | if(event.type == SDL_QUIT){ 166 | running =0; 167 | } 168 | if(event.type == SDL_MOUSEMOTION){ 169 | ; 170 | } 171 | if(event.type == SDL_MOUSEBUTTONDOWN){ 172 | if(event.button.button == SDL_BUTTON_RIGHT){ 173 | box.data.frac.pos.y = (float)wh-(real_meter_y*distance_m)-box.data.frac.size.y; 174 | box.data.dst.y = ceilf(box.data.frac.pos.y); 175 | 176 | } 177 | if(event.button.button == SDL_BUTTON_MIDDLE){ 178 | ; 179 | } 180 | if(event.button.button==SDL_BUTTON_LEFT){ 181 | 182 | accumulator=0; 183 | Altitude=distance_m; 184 | set=1; 185 | break; 186 | } 187 | } 188 | if(event.type == SDL_KEYDOWN ){ 189 | switch(event.key.keysym.sym ){ 190 | case SDLK_ESCAPE: 191 | running =0; 192 | break; 193 | 194 | case SDLK_r: 195 | case SDLK_BACKSPACE: 196 | break; 197 | 198 | case SDLK_p: 199 | case SDLK_SPACE: 200 | break; 201 | 202 | default: 203 | break; 204 | } 205 | } 206 | } 207 | //END EVENT LOOP 208 | 209 | float newTime = time_(); 210 | float deltaTime = newTime - currentTime; 211 | currentTime = newTime; 212 | accumulator += deltaTime; 213 | if (set){ 214 | if (Altitude > 0.0f){ 215 | //Box Altitude dropping over time 216 | Altitude = distance_m - 0.5 * g * accumulator * accumulator; 217 | SDL_Log("Altitude: %f", Altitude); 218 | SDL_Log("accumulator: %f", accumulator); 219 | 220 | box.data.frac.pos.y = (float)wh-(real_meter_y*Altitude)-box.data.frac.size.y; 221 | box.data.dst.y = ceilf(box.data.frac.pos.y); 222 | } 223 | } 224 | 225 | 226 | 227 | 228 | 229 | //BEGIN RENDERING 230 | SDL_SetRenderDrawColor(Renderer, BG); 231 | SDL_RenderClear(Renderer); 232 | SDL_RenderCopy(Renderer, tower_crane.Texture, NULL, &tower_crane.dst); 233 | SDL_RenderCopy(Renderer, box.data.Texture, NULL, &box.data.dst); 234 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 235 | SDL_RenderPresent(Renderer); 236 | //END RENDERING 237 | 238 | } 239 | //END MAIN LOOP 240 | exit_(); 241 | return EXIT_SUCCESS; 242 | 243 | } 244 | //END MAIN FUNCTION 245 | 246 | //BEGIN FUNCTIONS 247 | void assets_in(void) 248 | { 249 | //BEGIN TOWER CRANE 250 | temp_surface = IMG_Load("./assets/gfx/tower_crane.png"); 251 | tower_crane.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 252 | tower_crane.frac.size.y = wh; 253 | tower_crane.frac.size.x = ww; 254 | tower_crane.frac.pos.y = 0; 255 | tower_crane.frac.pos.x = 0; 256 | rect_round(&tower_crane); 257 | //END TOWER_CRANE 258 | 259 | //How many pixels are one centimeter on the screen? 260 | screen_centimeter_x = (SCALESIZE_H * SCALE_RATIO / CENTI_PER_INCH * dpi.x) / REALSIZE_H; 261 | SDL_Log("pixels are one centimeter on the picture x: %f", screen_centimeter_x); 262 | screen_centimeter_y = (SCALESIZE_V * SCALE_RATIO / CENTI_PER_INCH * dpi.y) / REALSIZE_V; 263 | SDL_Log("pixels are one centimeter on the picture y: %f", screen_centimeter_y); 264 | 265 | //How many pixels are one real meter? 266 | float screen_size_x=SCALESIZE_H / CENTI_PER_INCH * dpi.x; //it is ww, just repeated for precision 267 | float screen_size_y=SCALESIZE_V / CENTI_PER_INCH * dpi.y; //it is wh, just repeated for precision 268 | 269 | SDL_Log("screen_size_y: %f", screen_size_y); 270 | 271 | real_meter_x=screen_size_x/REALSIZE_H; 272 | SDL_Log("real_meter_x in pixels: %f", real_meter_x); 273 | 274 | real_meter_y=screen_size_y/REALSIZE_V; 275 | SDL_Log("real_meter_y in pixels: %f", real_meter_y); 276 | 277 | //BEGIN TESTBOX 278 | temp_surface = IMG_Load("./assets/gfx/container.png"); 279 | box.data.Texture=SDL_CreateTextureFromSurface(Renderer, temp_surface); 280 | box.data.frac.size.y = real_meter_x*2.5; 281 | box.data.frac.size.x = real_meter_y*8; 282 | box.data.frac.pos.x = real_meter_x*67.5; 283 | 284 | box.data.frac.pos.y = real_meter_y*12.3; 285 | rect_round(&box.data); 286 | box.vel.y = 0; 287 | box.vel.x = 0; 288 | //END TESTBOX 289 | 290 | //falling distance in real meters 291 | distance_m=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y)/real_meter_y; 292 | SDL_Log("falling distance: %.2f m.", distance_m); 293 | 294 | distance_p=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y); 295 | SDL_Log("falling distance in pixel: %f", distance_p); 296 | 297 | // These are the expected results for our values: 298 | float fall_time = sqrtf(2 * distance_m / g); 299 | SDL_Log("fall time: %f seconds", fall_time); 300 | // t 3,0142 301 | // v 29,5596 m/s 302 | // 106,4147 km/h 303 | 304 | //BEGIN SB_DESC 305 | font=TTF_OpenFont("./assets/fonts/NimbusSanL-Regu.ttf", 16); 306 | SDL_Color color={BLACK}; 307 | char text[30]; 308 | sprintf(text, "Map scale = 1: %.0f cm",SCALE_RATIO*SCALE_UNITS); 309 | temp_surface=TTF_RenderText_Blended(font,text,color); 310 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 311 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 312 | sb_desc.dst.x=ww-sb_desc.dst.w-50; 313 | sb_desc.dst.y=25; 314 | //END SB_DESC 315 | } 316 | 317 | void assets_out(void) 318 | { 319 | SDL_DestroyTexture(tower_crane.Texture); 320 | SDL_DestroyTexture(box.data.Texture); 321 | TTF_CloseFont(font); 322 | } 323 | 324 | void query_disp (void) 325 | { 326 | int disp; 327 | 328 | disp=SDL_GetWindowDisplayIndex(Window); 329 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 330 | SDL_Log("hdpi: %f", dpi.x); 331 | ww=roundf(SCALESIZE_H / CENTI_PER_INCH * dpi.x); 332 | SDL_Log("vdpi: %f", dpi.y); 333 | wh=roundf(SCALESIZE_V / CENTI_PER_INCH * dpi.y); 334 | SDL_Log("w: %d, h: %d",ww,wh); 335 | SDL_SetWindowSize(Window, ww,wh); 336 | 337 | } 338 | 339 | float time_(void) 340 | { 341 | 342 | static Uint64 start = 0; 343 | static float frequency = 0; 344 | 345 | if (start==0){ 346 | start = SDL_GetPerformanceCounter(); 347 | frequency= (float)SDL_GetPerformanceFrequency(); 348 | return 0.0f; 349 | } 350 | 351 | Uint64 counter = 0; 352 | counter=SDL_GetPerformanceCounter(); 353 | return (((float)counter - (float)start) /frequency); 354 | } 355 | 356 | void rect_round(struct entity_static *s_entity) 357 | { 358 | s_entity->dst.x=roundf(s_entity->frac.pos.x); 359 | s_entity->dst.y=roundf(s_entity->frac.pos.y); 360 | s_entity->dst.w=roundf(s_entity->frac.size.x); 361 | s_entity->dst.h=roundf(s_entity->frac.size.y); 362 | } 363 | 364 | void earth_gravity(void) 365 | { 366 | //calculating the accelleration with which an object is moving towards 367 | //to the centre of the earth 368 | float earth_mass = 5.9736e+24; //in kg 369 | float earth_radius = 6.375e+6; //in km 370 | float gc = 6.674e-11; //gravitational constant 371 | g=(gc*earth_mass)/(earth_radius*earth_radius); 372 | } 373 | //END FUNCTIONS 374 | -------------------------------------------------------------------------------- /src/4a.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * 6 | * Gravity 2 7 | * Adding Accumulated Time. 8 | */ 9 | 10 | //END DESCRIPTION 11 | 12 | //BEGIN INCLUDES 13 | //system headers 14 | #include 15 | //local headers 16 | #include "helper.h" 17 | //END INCLUDES 18 | 19 | //BEGIN CPP DEFINITIONS 20 | //Some Color Helpers 21 | //R G B 22 | #define RED 255,0,0,255 23 | #define GREEN 0,255,0,255 24 | #define BLUE 0,0,255,255 25 | #define BG 112,170,216,255 26 | //All on 27 | #define WHITE 255,255,255,255 28 | //All off 29 | #define BLACK 0,0,0,255 30 | 31 | //Real Size in SI-Unit meters 32 | #define REALSIZE_H 82.1305 33 | #define REALSIZE_V 52.8554 34 | 35 | #define SCALE_RATIO 4.0 36 | 37 | //Real Size in SI-Unit centimeters 38 | #define SCALE_UNITS 100.0 39 | 40 | #define SCALESIZE_H REALSIZE_H/SCALE_RATIO 41 | #define SCALESIZE_V REALSIZE_V/SCALE_RATIO 42 | 43 | //DPI is Dots per Inch - Industry should fix that 44 | #define CENTI_PER_INCH 2.54 45 | //END CPP DEFINITIONS 46 | 47 | //BEGIN DATASTRUCTURES 48 | struct vec2{ 49 | float x; 50 | float y; 51 | }; 52 | 53 | struct rect{ 54 | struct vec2 pos; 55 | struct vec2 size; 56 | }; 57 | 58 | struct entity_static{ 59 | SDL_Texture *Texture; 60 | struct rect frac; 61 | SDL_Rect dst; 62 | }; 63 | 64 | struct entity_dyn{ 65 | struct entity_static data; 66 | struct vec2 vel; 67 | }; 68 | //END DATASTRUCTURES 69 | 70 | //BEGIN GLOBALS 71 | int ww; 72 | int wh; 73 | 74 | int IARH; //Image Aspect Ratio horizontal 75 | int IARW; //Image Aspect Ratio vertical 76 | 77 | float g; 78 | struct vec2 dpi; 79 | 80 | float screen_centimeter_x; 81 | float screen_centimeter_y; 82 | 83 | float real_meter_x; 84 | float real_meter_y; 85 | 86 | float distance_m; 87 | float distance_p; 88 | 89 | SDL_Surface *temp_surface = NULL; 90 | //BEGIN VISIBLES 91 | 92 | SDL_Color color={BLACK}; 93 | TTF_Font *font = NULL; 94 | struct entity_static sb_desc; 95 | struct entity_static tower_crane; 96 | struct entity_dyn box; 97 | struct entity_static time_desc; 98 | struct entity_static time_desc_number; 99 | //END VISIBLES 100 | 101 | //END GLOBALS 102 | 103 | //BEGIN FUNCTION PROTOTYPES 104 | void query_disp (void); 105 | void assets_in (void); 106 | void assets_out (void); 107 | float time_ (void); 108 | void render_time (float); 109 | void rect_round (struct entity_static *s_entity); 110 | void earth_gravity(void); 111 | //END FUNCTION PROTOTYPES 112 | 113 | //END HEAD 114 | 115 | //BEGIN MAIN FUNCTION 116 | int main(int argc, char *argv[]) 117 | { 118 | 119 | (void)argc; 120 | (void)argv; 121 | 122 | //BEGIN INIT 123 | 124 | init(); 125 | query_disp(); 126 | earth_gravity(); 127 | SDL_Log("Gravity: %.2f m/s²", g); 128 | assets_in(); 129 | 130 | //BEGIN WINDOW 131 | 132 | char title[20]; 133 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 134 | sprintf(title, "%.2f x %.2f cm",SCALESIZE_H,SCALESIZE_V); 135 | SDL_SetWindowTitle(Window, title); 136 | SDL_ShowWindow(Window); 137 | 138 | //END WINDOW 139 | float currentTime = 0.0f; 140 | float accumulator = 0.0f; 141 | 142 | SDL_Event event; 143 | int running = 1; 144 | float Altitude=distance_m; 145 | //END INIT 146 | 147 | //BEGIN MAIN LOOP 148 | while(running){ 149 | static char set = 0; 150 | //BEGIN EVENT LOOP 151 | while(SDL_PollEvent(&event)){ 152 | if(event.type == SDL_QUIT){ 153 | running =0; 154 | } 155 | if(event.type == SDL_MOUSEMOTION){ 156 | ; 157 | } 158 | if(event.type == SDL_MOUSEBUTTONDOWN){ 159 | if(event.button.button == SDL_BUTTON_RIGHT){ 160 | box.data.frac.pos.y = (float)wh-(real_meter_y*distance_m)-box.data.frac.size.y; 161 | box.data.dst.y = ceilf(box.data.frac.pos.y); 162 | 163 | } 164 | if(event.button.button == SDL_BUTTON_MIDDLE){ 165 | ; 166 | } 167 | if(event.button.button==SDL_BUTTON_LEFT){ 168 | 169 | accumulator=0; 170 | Altitude=distance_m; 171 | set=1; 172 | break; 173 | } 174 | } 175 | if(event.type == SDL_KEYDOWN ){ 176 | switch(event.key.keysym.sym ){ 177 | case SDLK_ESCAPE: 178 | running =0; 179 | break; 180 | 181 | case SDLK_r: 182 | case SDLK_BACKSPACE: 183 | break; 184 | 185 | case SDLK_p: 186 | case SDLK_SPACE: 187 | break; 188 | 189 | default: 190 | break; 191 | } 192 | } 193 | } 194 | //END EVENT LOOP 195 | 196 | float newTime = time_(); 197 | float deltaTime = newTime - currentTime; 198 | currentTime = newTime; 199 | 200 | if (set){ 201 | if (Altitude > 0.0f){ 202 | accumulator += deltaTime; 203 | 204 | //Box Altitude dropping over time 205 | Altitude = distance_m - 0.5 * g * accumulator * accumulator; 206 | SDL_Log("Altitude: %f", Altitude); 207 | SDL_Log("accumulator: %f", accumulator); 208 | render_time(accumulator); 209 | box.data.frac.pos.y = (float)wh-(real_meter_y*Altitude)-box.data.frac.size.y; 210 | box.data.dst.y = ceilf(box.data.frac.pos.y); 211 | } 212 | } 213 | 214 | 215 | 216 | 217 | 218 | //BEGIN RENDERING 219 | SDL_SetRenderDrawColor(Renderer, BG); 220 | SDL_RenderClear(Renderer); 221 | SDL_RenderCopy(Renderer, tower_crane.Texture, NULL, &tower_crane.dst); 222 | SDL_RenderCopy(Renderer, box.data.Texture, NULL, &box.data.dst); 223 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 224 | SDL_RenderCopy(Renderer, time_desc.Texture, NULL, &time_desc.dst); 225 | SDL_RenderCopy(Renderer, time_desc_number.Texture, NULL, &time_desc_number.dst); 226 | SDL_RenderPresent(Renderer); 227 | //END RENDERING 228 | 229 | } 230 | //END MAIN LOOP 231 | exit_(); 232 | return EXIT_SUCCESS; 233 | 234 | } 235 | //END MAIN FUNCTION 236 | 237 | //BEGIN FUNCTIONS 238 | void assets_in(void) 239 | { 240 | //BEGIN TOWER CRANE 241 | temp_surface = IMG_Load("./assets/gfx/tower_crane.png"); 242 | tower_crane.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 243 | tower_crane.frac.size.y = wh; 244 | tower_crane.frac.size.x = ww; 245 | tower_crane.frac.pos.y = 0; 246 | tower_crane.frac.pos.x = 0; 247 | rect_round(&tower_crane); 248 | //END TOWER_CRANE 249 | 250 | //How many pixels are one centimeter on the screen? 251 | screen_centimeter_x = (SCALESIZE_H * SCALE_RATIO / CENTI_PER_INCH * dpi.x) / REALSIZE_H; 252 | SDL_Log("pixels are one centimeter on the picture x: %f", screen_centimeter_x); 253 | screen_centimeter_y = (SCALESIZE_V * SCALE_RATIO / CENTI_PER_INCH * dpi.y) / REALSIZE_V; 254 | SDL_Log("pixels are one centimeter on the picture y: %f", screen_centimeter_y); 255 | 256 | //How many pixels are one real meter? 257 | float screen_size_x=SCALESIZE_H / CENTI_PER_INCH * dpi.x; //it is ww, just repeated for precision 258 | float screen_size_y=SCALESIZE_V / CENTI_PER_INCH * dpi.y; //it is wh, just repeated for precision 259 | 260 | SDL_Log("screen_size_y: %f", screen_size_y); 261 | 262 | real_meter_x=screen_size_x/REALSIZE_H; 263 | SDL_Log("real_meter_x in pixels: %f", real_meter_x); 264 | 265 | real_meter_y=screen_size_y/REALSIZE_V; 266 | SDL_Log("real_meter_y in pixels: %f", real_meter_y); 267 | 268 | //BEGIN TESTBOX 269 | temp_surface = IMG_Load("./assets/gfx/container.png"); 270 | box.data.Texture=SDL_CreateTextureFromSurface(Renderer, temp_surface); 271 | box.data.frac.size.y = real_meter_x*2.5; 272 | box.data.frac.size.x = real_meter_y*8; 273 | box.data.frac.pos.x = real_meter_x*67.5; 274 | 275 | box.data.frac.pos.y = real_meter_y*12.3; 276 | rect_round(&box.data); 277 | box.vel.y = 0; 278 | box.vel.x = 0; 279 | //END TESTBOX 280 | 281 | //falling distance in real meters 282 | distance_m=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y)/real_meter_y; 283 | SDL_Log("falling distance: %.2f m.", distance_m); 284 | 285 | distance_p=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y); 286 | SDL_Log("falling distance in pixel: %f", distance_p); 287 | 288 | // These are the expected results for our values: 289 | float fall_time = sqrtf(2 * distance_m / g); 290 | SDL_Log("fall time: %f seconds", fall_time); 291 | // t 3,0142 292 | // v 29,5596 m/s 293 | // 106,4147 km/h 294 | 295 | //BEGIN SB_DESC 296 | font=TTF_OpenFont("./assets/fonts/NimbusSanL-Regu.ttf", 16); 297 | char text[30]; 298 | sprintf(text, "Map scale = 1: %.0f cm",SCALE_RATIO*SCALE_UNITS); 299 | temp_surface=TTF_RenderText_Blended(font,text,color); 300 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 301 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 302 | sb_desc.dst.x=ww-sb_desc.dst.w-50; 303 | sb_desc.dst.y=25; 304 | //END SB_DESC 305 | 306 | //BEGIN TIME_DESC 307 | sprintf(text, "Accumulated Time:"); 308 | temp_surface=TTF_RenderText_Blended(font,text,color); 309 | time_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 310 | SDL_QueryTexture(time_desc.Texture, NULL, NULL, &time_desc.dst.w, &time_desc.dst.h); 311 | time_desc.dst.x=ww/3; 312 | time_desc.dst.y=100; 313 | 314 | sprintf(text, "0.00"); 315 | temp_surface=TTF_RenderText_Blended(font,text,color); 316 | time_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 317 | SDL_QueryTexture(time_desc_number.Texture, NULL, NULL, &time_desc_number.dst.w, &time_desc_number.dst.h); 318 | time_desc_number.dst.x=time_desc.dst.x+time_desc.dst.w+(time_desc.dst.h/5); 319 | time_desc_number.dst.y=time_desc.dst.y; 320 | //END TIME_DESC 321 | } 322 | 323 | void assets_out(void) 324 | { 325 | SDL_DestroyTexture(tower_crane.Texture); 326 | SDL_DestroyTexture(sb_desc.Texture); 327 | SDL_DestroyTexture(box.data.Texture); 328 | SDL_DestroyTexture(time_desc.Texture); 329 | SDL_DestroyTexture(time_desc_number.Texture); 330 | TTF_CloseFont(font); 331 | } 332 | 333 | void query_disp (void) 334 | { 335 | int disp; 336 | 337 | disp=SDL_GetWindowDisplayIndex(Window); 338 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 339 | SDL_Log("hdpi: %f", dpi.x); 340 | ww=roundf(SCALESIZE_H / CENTI_PER_INCH * dpi.x); 341 | SDL_Log("vdpi: %f", dpi.y); 342 | wh=roundf(SCALESIZE_V / CENTI_PER_INCH * dpi.y); 343 | SDL_Log("w: %d, h: %d",ww,wh); 344 | SDL_SetWindowSize(Window, ww,wh); 345 | 346 | } 347 | 348 | void render_time (float time) 349 | { 350 | char buffer[10]; 351 | sprintf(buffer, "%.2f", time); 352 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 353 | time_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 354 | SDL_QueryTexture(time_desc_number.Texture, NULL, NULL, &time_desc_number.dst.w, &time_desc_number.dst.h); 355 | } 356 | 357 | float time_(void) 358 | { 359 | 360 | static Uint64 start = 0; 361 | static float frequency = 0; 362 | 363 | if (start==0){ 364 | start = SDL_GetPerformanceCounter(); 365 | frequency= (float)SDL_GetPerformanceFrequency(); 366 | return 0.0f; 367 | } 368 | 369 | Uint64 counter = 0; 370 | counter=SDL_GetPerformanceCounter(); 371 | return (((float)counter - (float)start) /frequency); 372 | } 373 | 374 | void rect_round(struct entity_static *s_entity) 375 | { 376 | s_entity->dst.x=roundf(s_entity->frac.pos.x); 377 | s_entity->dst.y=roundf(s_entity->frac.pos.y); 378 | s_entity->dst.w=roundf(s_entity->frac.size.x); 379 | s_entity->dst.h=roundf(s_entity->frac.size.y); 380 | } 381 | 382 | void earth_gravity(void) 383 | { 384 | //calculating the accelleration with which an object is moving towards 385 | //to the centre of the earth 386 | float earth_mass = 5.9736e+24; //in kg 387 | float earth_radius = 6.375e+6; //in km 388 | float gc = 6.674e-11; //gravitational constant 389 | g=(gc*earth_mass)/(earth_radius*earth_radius); 390 | } 391 | //END FUNCTIONS 392 | -------------------------------------------------------------------------------- /src/4b.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * 6 | * Gravity 3 7 | * Adding Velocity to Screen 8 | */ 9 | 10 | //END DESCRIPTION 11 | 12 | //BEGIN INCLUDES 13 | //system headers 14 | #include 15 | //local headers 16 | #include "helper.h" 17 | //END INCLUDES 18 | 19 | //BEGIN CPP DEFINITIONS 20 | //Some Color Helpers 21 | //R G B 22 | #define RED 255,0,0,255 23 | #define GREEN 0,255,0,255 24 | #define BLUE 0,0,255,255 25 | #define BG 112,170,216,255 26 | //All on 27 | #define WHITE 255,255,255,255 28 | //All off 29 | #define BLACK 0,0,0,255 30 | 31 | //Real Size in SI-Unit meters 32 | #define REALSIZE_H 82.1305 33 | #define REALSIZE_V 52.8554 34 | 35 | #define SCALE_RATIO 4.0 36 | 37 | //Real Size in SI-Unit centimeters 38 | #define SCALE_UNITS 100.0 39 | 40 | #define SCALESIZE_H REALSIZE_H/SCALE_RATIO 41 | #define SCALESIZE_V REALSIZE_V/SCALE_RATIO 42 | 43 | //DPI is Dots per Inch - Industry should fix that 44 | #define CENTI_PER_INCH 2.54 45 | //END CPP DEFINITIONS 46 | 47 | //BEGIN DATASTRUCTURES 48 | struct vec2{ 49 | float x; 50 | float y; 51 | }; 52 | 53 | struct rect{ 54 | struct vec2 pos; 55 | struct vec2 size; 56 | }; 57 | 58 | struct entity_static{ 59 | SDL_Texture *Texture; 60 | struct rect frac; 61 | SDL_Rect dst; 62 | }; 63 | 64 | struct entity_dyn{ 65 | struct entity_static data; 66 | struct vec2 vel; 67 | }; 68 | //END DATASTRUCTURES 69 | 70 | //BEGIN GLOBALS 71 | int ww; 72 | int wh; 73 | 74 | int IARH; //Image Aspect Ratio horizontal 75 | int IARW; //Image Aspect Ratio vertical 76 | 77 | float g; // Gravity 78 | float v; // Velocity 79 | struct vec2 dpi; 80 | 81 | float screen_centimeter_x; 82 | float screen_centimeter_y; 83 | 84 | float real_meter_x; 85 | float real_meter_y; 86 | 87 | float distance_m; 88 | float distance_p; 89 | 90 | SDL_Surface *temp_surface = NULL; 91 | //BEGIN VISIBLES 92 | 93 | SDL_Color color={BLACK}; 94 | TTF_Font *font = NULL; 95 | struct entity_static sb_desc; 96 | struct entity_static tower_crane; 97 | struct entity_dyn box; 98 | struct entity_static time_desc; 99 | struct entity_static time_desc_number; 100 | struct entity_static vel_desc; 101 | struct entity_static vel_desc_number; 102 | //END VISIBLES 103 | 104 | //END GLOBALS 105 | 106 | //BEGIN FUNCTION PROTOTYPES 107 | void query_disp (void); 108 | void assets_in (void); 109 | void assets_out (void); 110 | float time_ (void); 111 | void render_time_and_vel (float); 112 | void rect_round (struct entity_static *s_entity); 113 | void earth_gravity(void); 114 | //END FUNCTION PROTOTYPES 115 | 116 | //END HEAD 117 | 118 | //BEGIN MAIN FUNCTION 119 | int main(int argc, char *argv[]) 120 | { 121 | 122 | (void)argc; 123 | (void)argv; 124 | 125 | //BEGIN INIT 126 | 127 | init(); 128 | query_disp(); 129 | earth_gravity(); 130 | SDL_Log("Gravity: %.2f m/s²", g); 131 | assets_in(); 132 | 133 | //BEGIN WINDOW 134 | 135 | char title[20]; 136 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 137 | sprintf(title, "%.2f x %.2f cm",SCALESIZE_H,SCALESIZE_V); 138 | SDL_SetWindowTitle(Window, title); 139 | SDL_ShowWindow(Window); 140 | 141 | //END WINDOW 142 | float currentTime = 0.0f; 143 | float accumulator = 0.0f; 144 | 145 | SDL_Event event; 146 | int running = 1; 147 | float Altitude=distance_m; 148 | //END INIT 149 | 150 | //BEGIN MAIN LOOP 151 | while(running){ 152 | static char set = 0; 153 | //BEGIN EVENT LOOP 154 | while(SDL_PollEvent(&event)){ 155 | if(event.type == SDL_QUIT){ 156 | running =0; 157 | } 158 | if(event.type == SDL_MOUSEMOTION){ 159 | ; 160 | } 161 | if(event.type == SDL_MOUSEBUTTONDOWN){ 162 | if(event.button.button == SDL_BUTTON_RIGHT){ 163 | box.data.frac.pos.y = (float)wh-(real_meter_y*distance_m)-box.data.frac.size.y; 164 | box.data.dst.y = ceilf(box.data.frac.pos.y); 165 | 166 | } 167 | if(event.button.button == SDL_BUTTON_MIDDLE){ 168 | ; 169 | } 170 | if(event.button.button==SDL_BUTTON_LEFT){ 171 | 172 | accumulator=0; 173 | Altitude=distance_m; 174 | set=1; 175 | break; 176 | } 177 | } 178 | if(event.type == SDL_KEYDOWN ){ 179 | switch(event.key.keysym.sym ){ 180 | case SDLK_ESCAPE: 181 | running =0; 182 | break; 183 | 184 | case SDLK_r: 185 | case SDLK_BACKSPACE: 186 | break; 187 | 188 | case SDLK_p: 189 | case SDLK_SPACE: 190 | break; 191 | 192 | default: 193 | break; 194 | } 195 | } 196 | } 197 | //END EVENT LOOP 198 | 199 | float newTime = time_(); 200 | float deltaTime = newTime - currentTime; 201 | currentTime = newTime; 202 | 203 | if (set){ 204 | if (Altitude > 0.0f){ 205 | accumulator += deltaTime; 206 | 207 | //Box Altitude dropping over time 208 | Altitude = distance_m - 0.5 * g * accumulator * accumulator; 209 | SDL_Log("Altitude: %f", Altitude); 210 | SDL_Log("accumulator: %f", accumulator); 211 | render_time_and_vel(accumulator); 212 | box.data.frac.pos.y = (float)wh-(real_meter_y*Altitude)-box.data.frac.size.y; 213 | box.data.dst.y = ceilf(box.data.frac.pos.y); 214 | } 215 | } 216 | 217 | 218 | 219 | 220 | 221 | //BEGIN RENDERING 222 | SDL_SetRenderDrawColor(Renderer, BG); 223 | SDL_RenderClear(Renderer); 224 | SDL_RenderCopy(Renderer, tower_crane.Texture, NULL, &tower_crane.dst); 225 | SDL_RenderCopy(Renderer, box.data.Texture, NULL, &box.data.dst); 226 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 227 | SDL_RenderCopy(Renderer, time_desc.Texture, NULL, &time_desc.dst); 228 | SDL_RenderCopy(Renderer, time_desc_number.Texture, NULL, &time_desc_number.dst); 229 | SDL_RenderCopy(Renderer, vel_desc.Texture, NULL, &vel_desc.dst); 230 | SDL_RenderCopy(Renderer, vel_desc_number.Texture, NULL, &vel_desc_number.dst); 231 | SDL_RenderPresent(Renderer); 232 | //END RENDERING 233 | 234 | } 235 | //END MAIN LOOP 236 | exit_(); 237 | return EXIT_SUCCESS; 238 | 239 | } 240 | //END MAIN FUNCTION 241 | 242 | //BEGIN FUNCTIONS 243 | void assets_in(void) 244 | { 245 | //BEGIN TOWER CRANE 246 | temp_surface = IMG_Load("./assets/gfx/tower_crane.png"); 247 | tower_crane.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 248 | tower_crane.frac.size.y = wh; 249 | tower_crane.frac.size.x = ww; 250 | tower_crane.frac.pos.y = 0; 251 | tower_crane.frac.pos.x = 0; 252 | rect_round(&tower_crane); 253 | //END TOWER_CRANE 254 | 255 | //How many pixels are one centimeter on the screen? 256 | screen_centimeter_x = (SCALESIZE_H * SCALE_RATIO / CENTI_PER_INCH * dpi.x) / REALSIZE_H; 257 | SDL_Log("pixels are one centimeter on the picture x: %f", screen_centimeter_x); 258 | screen_centimeter_y = (SCALESIZE_V * SCALE_RATIO / CENTI_PER_INCH * dpi.y) / REALSIZE_V; 259 | SDL_Log("pixels are one centimeter on the picture y: %f", screen_centimeter_y); 260 | 261 | //How many pixels are one real meter? 262 | float screen_size_x=SCALESIZE_H / CENTI_PER_INCH * dpi.x; //it is ww, just repeated for precision 263 | float screen_size_y=SCALESIZE_V / CENTI_PER_INCH * dpi.y; //it is wh, just repeated for precision 264 | 265 | SDL_Log("screen_size_y: %f", screen_size_y); 266 | 267 | real_meter_x=screen_size_x/REALSIZE_H; 268 | SDL_Log("real_meter_x in pixels: %f", real_meter_x); 269 | 270 | real_meter_y=screen_size_y/REALSIZE_V; 271 | SDL_Log("real_meter_y in pixels: %f", real_meter_y); 272 | 273 | //BEGIN TESTBOX 274 | temp_surface = IMG_Load("./assets/gfx/container.png"); 275 | box.data.Texture=SDL_CreateTextureFromSurface(Renderer, temp_surface); 276 | box.data.frac.size.y = real_meter_x*2.5; 277 | box.data.frac.size.x = real_meter_y*8; 278 | box.data.frac.pos.x = real_meter_x*67.5; 279 | 280 | box.data.frac.pos.y = real_meter_y*12.3; 281 | rect_round(&box.data); 282 | box.vel.y = 0; 283 | box.vel.x = 0; 284 | //END TESTBOX 285 | 286 | //falling distance in real meters 287 | distance_m=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y)/real_meter_y; 288 | SDL_Log("falling distance: %.2f m.", distance_m); 289 | 290 | distance_p=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y); 291 | SDL_Log("falling distance in pixel: %f", distance_p); 292 | 293 | // These are the expected results for our values: 294 | float fall_time = sqrtf(2 * distance_m / g); 295 | SDL_Log("fall time: %f seconds", fall_time); 296 | // t 3,0142 297 | // v 29,5596 m/s 298 | // 106,4147 km/h 299 | 300 | //BEGIN SB_DESC 301 | font=TTF_OpenFont("./assets/fonts/FiraCode-Regular.ttf", 16); 302 | char text[30]; 303 | sprintf(text, "Map scale = 1: %.0f cm",SCALE_RATIO*SCALE_UNITS); 304 | temp_surface=TTF_RenderText_Blended(font,text,color); 305 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 306 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 307 | sb_desc.dst.x=ww-sb_desc.dst.w-50; 308 | sb_desc.dst.y=25; 309 | //END SB_DESC 310 | 311 | //BEGIN TIME_DESC 312 | sprintf(text, "Accumulated Time:"); 313 | temp_surface=TTF_RenderText_Blended(font,text,color); 314 | time_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 315 | SDL_QueryTexture(time_desc.Texture, NULL, NULL, &time_desc.dst.w, &time_desc.dst.h); 316 | time_desc.dst.x=ww/3; 317 | time_desc.dst.y=100; 318 | 319 | sprintf(text, "00.00"); 320 | temp_surface=TTF_RenderText_Blended(font,text,color); 321 | time_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 322 | SDL_QueryTexture(time_desc_number.Texture, NULL, NULL, &time_desc_number.dst.w, &time_desc_number.dst.h); 323 | time_desc_number.dst.x=time_desc.dst.x+time_desc.dst.w+(time_desc.dst.h/5); 324 | time_desc_number.dst.y=time_desc.dst.y; 325 | //END TIME_DESC 326 | 327 | //BEGIN VEL_DESC 328 | sprintf(text, "Vel:"); 329 | temp_surface=TTF_RenderText_Blended(font,text,color); 330 | vel_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 331 | SDL_QueryTexture(vel_desc.Texture, NULL, NULL, &vel_desc.dst.w, &vel_desc.dst.h); 332 | vel_desc.dst.x=time_desc.dst.x+time_desc.dst.w-vel_desc.dst.w; 333 | vel_desc.dst.y=125; 334 | 335 | sprintf(text, "00.00"); 336 | temp_surface=TTF_RenderText_Blended(font,text,color); 337 | vel_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 338 | SDL_QueryTexture(vel_desc_number.Texture, NULL, NULL, &vel_desc_number.dst.w, &vel_desc_number.dst.h); 339 | vel_desc_number.dst.x=vel_desc.dst.x+vel_desc.dst.w+(vel_desc.dst.h/5); 340 | vel_desc_number.dst.y=vel_desc.dst.y; 341 | //END VEL_DESC 342 | 343 | } 344 | 345 | void assets_out(void) 346 | { 347 | SDL_DestroyTexture(tower_crane.Texture); 348 | SDL_DestroyTexture(sb_desc.Texture); 349 | SDL_DestroyTexture(box.data.Texture); 350 | SDL_DestroyTexture(time_desc.Texture); 351 | SDL_DestroyTexture(time_desc_number.Texture); 352 | TTF_CloseFont(font); 353 | } 354 | 355 | void query_disp (void) 356 | { 357 | int disp; 358 | 359 | disp=SDL_GetWindowDisplayIndex(Window); 360 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 361 | SDL_Log("hdpi: %f", dpi.x); 362 | ww=roundf(SCALESIZE_H / CENTI_PER_INCH * dpi.x); 363 | SDL_Log("vdpi: %f", dpi.y); 364 | wh=roundf(SCALESIZE_V / CENTI_PER_INCH * dpi.y); 365 | SDL_Log("w: %d, h: %d",ww,wh); 366 | SDL_SetWindowSize(Window, ww,wh); 367 | 368 | } 369 | 370 | void render_time_and_vel (float time) 371 | { 372 | char buffer[10]; 373 | sprintf(buffer, "%05.2f", time); 374 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 375 | time_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 376 | SDL_QueryTexture(time_desc_number.Texture, NULL, NULL, &time_desc_number.dst.w, &time_desc_number.dst.h); 377 | 378 | v = g * time; 379 | sprintf(buffer, "%05.2f", v); 380 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 381 | vel_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 382 | SDL_QueryTexture(vel_desc_number.Texture, NULL, NULL, &vel_desc_number.dst.w, &vel_desc_number.dst.h); 383 | } 384 | 385 | float time_(void) 386 | { 387 | 388 | static Uint64 start = 0; 389 | static float frequency = 0; 390 | 391 | if (start==0){ 392 | start = SDL_GetPerformanceCounter(); 393 | frequency= (float)SDL_GetPerformanceFrequency(); 394 | return 0.0f; 395 | } 396 | 397 | Uint64 counter = 0; 398 | counter=SDL_GetPerformanceCounter(); 399 | return (((float)counter - (float)start) /frequency); 400 | } 401 | 402 | void rect_round(struct entity_static *s_entity) 403 | { 404 | s_entity->dst.x=roundf(s_entity->frac.pos.x); 405 | s_entity->dst.y=roundf(s_entity->frac.pos.y); 406 | s_entity->dst.w=roundf(s_entity->frac.size.x); 407 | s_entity->dst.h=roundf(s_entity->frac.size.y); 408 | } 409 | 410 | void earth_gravity(void) 411 | { 412 | //calculating the accelleration with which an object is moving towards 413 | //to the centre of the earth 414 | float earth_mass = 5.9736e+24; //in kg 415 | float earth_radius = 6.375e+6; //in km 416 | float gc = 6.674e-11; //gravitational constant 417 | g=(gc*earth_mass)/(earth_radius*earth_radius); 418 | } 419 | //END FUNCTIONS 420 | -------------------------------------------------------------------------------- /src/4c.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | 3 | //BEGIN DESCRIPTION 4 | 5 | /* DESCRIPTION: 6 | * 7 | * Gravity 4 8 | * Adding: Acceleration and Altitude to Screen 9 | * Pauseable 10 | */ 11 | 12 | /* TODO: 13 | * 14 | * Scale Window 15 | * 16 | */ 17 | //END DESCRIPTION 18 | 19 | //BEGIN INCLUDES 20 | //system headers 21 | #include 22 | //local headers 23 | #include "helper.h" 24 | //END INCLUDES 25 | 26 | //BEGIN CPP DEFINITIONS 27 | //Some Color Helpers 28 | //R G B 29 | #define RED 255,0,0,255 30 | #define GREEN 0,255,0,255 31 | #define BLUE 0,0,255,255 32 | #define BG 112,170,216,255 33 | //All on 34 | #define WHITE 255,255,255,255 35 | //All off 36 | #define BLACK 0,0,0,255 37 | 38 | //Real Size in SI-Unit meters 39 | #define REALSIZE_H 82.1305 40 | #define REALSIZE_V 52.8554 41 | 42 | #define SCALE_RATIO 4.0 43 | 44 | //Real Size in SI-Unit centimeters 45 | #define SCALE_UNITS 100.0 46 | 47 | #define SCALESIZE_H REALSIZE_H/SCALE_RATIO 48 | #define SCALESIZE_V REALSIZE_V/SCALE_RATIO 49 | 50 | //DPI is Dots per Inch - Industry should fix that 51 | #define CENTI_PER_INCH 2.54 52 | //END CPP DEFINITIONS 53 | 54 | //BEGIN DATASTRUCTURES 55 | struct vec2{ 56 | float x; 57 | float y; 58 | }; 59 | 60 | struct rect{ 61 | struct vec2 pos; 62 | struct vec2 size; 63 | }; 64 | 65 | struct entity_static{ 66 | SDL_Texture *Texture; 67 | struct rect frac; 68 | SDL_Rect dst; 69 | }; 70 | 71 | struct entity_dyn{ 72 | struct entity_static data; 73 | struct vec2 vel; 74 | }; 75 | //END DATASTRUCTURES 76 | 77 | //BEGIN GLOBALS 78 | 79 | //BEGIN WINDOW AND SCALING 80 | int ww; 81 | int wh; 82 | 83 | int IARH; // Image Aspect Ratio horizontal 84 | int IARW; // Image Aspect Ratio vertical 85 | 86 | struct vec2 dpi; 87 | 88 | float screen_centimeter_x; 89 | float screen_centimeter_y; 90 | 91 | float real_meter_x; 92 | float real_meter_y; 93 | //END WINDOW AND SCALING 94 | 95 | 96 | float g; // Gravity 97 | float v; // Velocity 98 | float a; // Acceleration 99 | float z; // Height 100 | 101 | float distance_m; 102 | float distance_p; 103 | 104 | 105 | 106 | //BEGIN VISIBLES 107 | SDL_Surface *temp_surface = NULL; 108 | SDL_Color color={BLACK}; 109 | TTF_Font *font = NULL; 110 | struct entity_static sb_desc; 111 | struct entity_static sbr[8]; 112 | struct entity_static tower_crane; 113 | struct entity_dyn box; 114 | struct entity_static time_desc; 115 | struct entity_static time_desc_number; 116 | struct entity_static vel_desc; 117 | struct entity_static vel_desc_number; 118 | struct entity_static acc_desc; 119 | struct entity_static acc_desc_number; 120 | struct entity_static alt_desc; 121 | struct entity_static alt_desc_number; 122 | //END VISIBLES 123 | 124 | //END GLOBALS 125 | 126 | //BEGIN FUNCTION PROTOTYPES 127 | void query_disp (void); 128 | void assets_in (void); 129 | void assets_out (void); 130 | float time_ (void); 131 | void update_desc (float, float); 132 | void rect_round (struct entity_static *s_entity); 133 | void earth_gravity (void); 134 | //END FUNCTION PROTOTYPES 135 | 136 | //END HEAD 137 | 138 | //BEGIN MAIN FUNCTION 139 | int main(int argc, char *argv[]) 140 | { 141 | 142 | //BEGIN INIT 143 | (void)argc; 144 | (void)argv; 145 | init(); 146 | query_disp(); 147 | earth_gravity(); 148 | SDL_Log("Gravity: %.2f m/s²", g); 149 | assets_in(); 150 | 151 | //BEGIN WINDOW 152 | 153 | char title[20]; 154 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 155 | sprintf(title, "%.2f x %.2f cm",SCALESIZE_H,SCALESIZE_V); 156 | SDL_SetWindowTitle(Window, title); 157 | SDL_ShowWindow(Window); 158 | 159 | //END WINDOW 160 | float currentTime = 0.0f; 161 | float accumulator = 0.0f; 162 | 163 | SDL_Event event; 164 | int running = 1; 165 | char pause = 0; 166 | float Altitude=distance_m; 167 | //END INIT 168 | 169 | //BEGIN MAIN LOOP 170 | while(running){ 171 | static char set = 0; 172 | //BEGIN EVENT LOOP 173 | while(SDL_PollEvent(&event)){ 174 | if(event.type == SDL_QUIT){ 175 | running =0; 176 | } 177 | if(event.type == SDL_MOUSEMOTION){ 178 | ; 179 | } 180 | if(event.type == SDL_MOUSEBUTTONDOWN){ 181 | if(event.button.button == SDL_BUTTON_RIGHT){ 182 | box.data.frac.pos.y = (float)wh-(real_meter_y*distance_m)-box.data.frac.size.y; 183 | box.data.dst.y = ceilf(box.data.frac.pos.y); 184 | 185 | } 186 | if(event.button.button == SDL_BUTTON_MIDDLE){ 187 | ; 188 | } 189 | if(event.button.button==SDL_BUTTON_LEFT){ 190 | 191 | accumulator=0; 192 | Altitude=distance_m; 193 | set=1; 194 | break; 195 | } 196 | } 197 | if(event.type == SDL_KEYDOWN ){ 198 | switch(event.key.keysym.sym ){ 199 | case SDLK_ESCAPE: 200 | running =0; 201 | break; 202 | 203 | case SDLK_r: 204 | case SDLK_BACKSPACE: 205 | break; 206 | 207 | case SDLK_p: 208 | pause=pause^1; //XOR - flip var 209 | if (pause) 210 | SDL_Log("pause"); 211 | else 212 | SDL_Log("unpaused"); 213 | case SDLK_SPACE: 214 | break; 215 | 216 | default: 217 | break; 218 | } 219 | } 220 | } 221 | //END EVENT LOOP 222 | 223 | float newTime = time_(); 224 | float deltaTime = newTime - currentTime; 225 | currentTime = newTime; 226 | if (set && !pause){ 227 | if (Altitude > 0.0f){ 228 | accumulator += deltaTime; 229 | Altitude = distance_m - 0.5 * g * accumulator * accumulator; 230 | update_desc (accumulator, Altitude); 231 | box.data.frac.pos.y = (float)wh-(real_meter_y*Altitude)-box.data.frac.size.y; 232 | box.data.dst.y = ceilf(box.data.frac.pos.y); 233 | } 234 | } 235 | 236 | //BEGIN RENDERING 237 | SDL_SetRenderDrawColor(Renderer, BG); 238 | SDL_RenderClear(Renderer); 239 | SDL_RenderCopy(Renderer, tower_crane.Texture, NULL, &tower_crane.dst); 240 | SDL_RenderCopy(Renderer, box.data.Texture, NULL, &box.data.dst); 241 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 242 | //BEGIN SCALING BAR 243 | for (int i=0; i<8; i++){ 244 | if (i%2){ 245 | SDL_SetRenderDrawColor(Renderer, WHITE); 246 | SDL_RenderFillRect(Renderer, &sbr[i].dst); 247 | SDL_SetRenderDrawColor(Renderer, BLACK); 248 | SDL_RenderDrawRect(Renderer, &sbr[i].dst); 249 | }else{ 250 | SDL_SetRenderDrawColor(Renderer, BLACK); 251 | SDL_RenderFillRect(Renderer, &sbr[i].dst); 252 | } 253 | } 254 | //END SCALING BAR 255 | SDL_RenderCopy(Renderer, time_desc.Texture, NULL, &time_desc.dst); 256 | SDL_RenderCopy(Renderer, time_desc_number.Texture, NULL, &time_desc_number.dst); 257 | SDL_RenderCopy(Renderer, vel_desc.Texture, NULL, &vel_desc.dst); 258 | SDL_RenderCopy(Renderer, vel_desc_number.Texture, NULL, &vel_desc_number.dst); 259 | SDL_RenderCopy(Renderer, acc_desc.Texture, NULL, &acc_desc.dst); 260 | SDL_RenderCopy(Renderer, acc_desc_number.Texture, NULL, &acc_desc_number.dst); 261 | SDL_RenderCopy(Renderer, alt_desc.Texture, NULL, &alt_desc.dst); 262 | SDL_RenderCopy(Renderer, alt_desc_number.Texture, NULL, &alt_desc_number.dst); 263 | SDL_RenderPresent(Renderer); 264 | //END RENDERING 265 | 266 | } 267 | //END MAIN LOOP 268 | 269 | //BEGIN EXIT 270 | assets_out(); 271 | exit_(); 272 | return EXIT_SUCCESS; 273 | //END EXIT 274 | 275 | } 276 | //END MAIN FUNCTION 277 | 278 | //BEGIN FUNCTIONS 279 | 280 | //BEGIN ASSETS AND INIT 281 | void assets_in(void) 282 | { 283 | //BEGIN TOWER CRANE 284 | temp_surface = IMG_Load("./assets/gfx/tower_crane.png"); 285 | tower_crane.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 286 | tower_crane.frac.size.y = wh; 287 | tower_crane.frac.size.x = ww; 288 | tower_crane.frac.pos.y = 0; 289 | tower_crane.frac.pos.x = 0; 290 | rect_round(&tower_crane); 291 | //END TOWER_CRANE 292 | 293 | //BEGIN SCALING 294 | //How many pixels are one centimeter on the screen? 295 | screen_centimeter_x = (SCALESIZE_H * SCALE_RATIO / CENTI_PER_INCH * dpi.x) / REALSIZE_H; 296 | SDL_Log("pixels are one centimeter on the picture x: %f", screen_centimeter_x); 297 | screen_centimeter_y = (SCALESIZE_V * SCALE_RATIO / CENTI_PER_INCH * dpi.y) / REALSIZE_V; 298 | SDL_Log("pixels are one centimeter on the picture y: %f", screen_centimeter_y); 299 | 300 | //How many pixels are one real meter? 301 | float screen_size_x=SCALESIZE_H / CENTI_PER_INCH * dpi.x; //it is ww, just repeated for precision 302 | float screen_size_y=SCALESIZE_V / CENTI_PER_INCH * dpi.y; //it is wh, just repeated for precision 303 | 304 | SDL_Log("screen_size_y: %f", screen_size_y); 305 | 306 | real_meter_x=screen_size_x/REALSIZE_H; 307 | SDL_Log("real_meter_x in pixels: %f", real_meter_x); 308 | 309 | real_meter_y=screen_size_y/REALSIZE_V; 310 | SDL_Log("real_meter_y in pixels: %f", real_meter_y); 311 | //END SCALING 312 | 313 | //BEGIN TESTBOX 314 | temp_surface = IMG_Load("./assets/gfx/container.png"); 315 | box.data.Texture=SDL_CreateTextureFromSurface(Renderer, temp_surface); 316 | box.data.frac.size.y = real_meter_x*2.5; 317 | box.data.frac.size.x = real_meter_y*8; 318 | box.data.frac.pos.x = real_meter_x*67.5; 319 | 320 | box.data.frac.pos.y = real_meter_y*12.3; 321 | rect_round(&box.data); 322 | box.vel.y = 0; 323 | box.vel.x = 0; 324 | //END TESTBOX 325 | 326 | //BEGIN SCALING2 327 | //falling distance in real meters 328 | distance_m=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y)/real_meter_y; 329 | SDL_Log("falling distance: %.2f m.", distance_m); 330 | 331 | distance_p=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y); 332 | SDL_Log("falling distance in pixel: %f", distance_p); 333 | 334 | // These are the expected results for our values: 335 | float fall_time = sqrtf(2 * distance_m / g); 336 | SDL_Log("fall time: %f seconds", fall_time); 337 | // t 3,0142 s 338 | // v 29,5596 m/s 339 | // v 106,4147 km/h 340 | //END SCALING2 341 | 342 | //BEGIN SB_DESC 343 | font=TTF_OpenFont("./assets/fonts/FiraCode-Regular.ttf", 16); 344 | char text[30]; 345 | sprintf(text, "Map scale = 1: %.0f cm",SCALE_RATIO*SCALE_UNITS); 346 | temp_surface=TTF_RenderText_Blended(font,text,color); 347 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 348 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 349 | sb_desc.dst.x=ww-sb_desc.dst.w-50; 350 | sb_desc.dst.y=25; 351 | //END SB_DESC 352 | 353 | //BEGIN SCALE BAR RECT 354 | sbr[0].dst.w = roundf(0.5 / CENTI_PER_INCH*dpi.x); 355 | sbr[0].dst.h = sbr[0].dst.w/3; 356 | // sbr[0].dst.x = sbr[0].dst.w*2; 357 | sbr[0].dst.x = sb_desc.dst.x; 358 | // sbr[0].dst.y = sbr[0].dst.w*4; 359 | sbr[0].dst.y = sb_desc.dst.y + sb_desc.dst.h + sb_desc.dst.h / 5; 360 | 361 | for (int i=1; i<8; i++){ 362 | sbr[i].dst.w=sbr[0].dst.w; 363 | sbr[i].dst.h=sbr[0].dst.h; 364 | sbr[i].dst.y=sbr[0].dst.y; 365 | 366 | sbr[i].dst.x=sbr[0].dst.x + (sbr[0].dst.w*i); 367 | } 368 | //END SCALE BAR RECT 369 | 370 | //BEGIN TIME_DESC 371 | sprintf(text, "Accumulated Time:"); 372 | temp_surface=TTF_RenderText_Blended(font,text,color); 373 | time_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 374 | SDL_QueryTexture(time_desc.Texture, NULL, NULL, &time_desc.dst.w, &time_desc.dst.h); 375 | time_desc.dst.x=ww/3; 376 | time_desc.dst.y=100; 377 | 378 | sprintf(text, "00.00"); 379 | temp_surface=TTF_RenderText_Blended(font,text,color); 380 | time_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 381 | SDL_QueryTexture(time_desc_number.Texture, NULL, NULL, &time_desc_number.dst.w, &time_desc_number.dst.h); 382 | time_desc_number.dst.x=time_desc.dst.x+time_desc.dst.w+(time_desc.dst.h/5); 383 | time_desc_number.dst.y=time_desc.dst.y; 384 | //END TIME_DESC 385 | 386 | //BEGIN VEL_DESC 387 | sprintf(text, "Vel in m/s:"); 388 | temp_surface=TTF_RenderText_Blended(font,text,color); 389 | vel_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 390 | SDL_QueryTexture(vel_desc.Texture, NULL, NULL, &vel_desc.dst.w, &vel_desc.dst.h); 391 | vel_desc.dst.x=time_desc.dst.x+time_desc.dst.w-vel_desc.dst.w; 392 | vel_desc.dst.y=125; 393 | 394 | sprintf(text, "00.00"); 395 | temp_surface=TTF_RenderText_Blended(font,text,color); 396 | vel_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 397 | SDL_QueryTexture(vel_desc_number.Texture, NULL, NULL, &vel_desc_number.dst.w, &vel_desc_number.dst.h); 398 | vel_desc_number.dst.x=vel_desc.dst.x+vel_desc.dst.w+(vel_desc.dst.h/5); 399 | vel_desc_number.dst.y=vel_desc.dst.y; 400 | //END VEL_DESC 401 | 402 | //BEGIN ACC_DESC 403 | sprintf(text, "Acc:"); 404 | temp_surface=TTF_RenderText_Blended(font,text,color); 405 | acc_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 406 | SDL_QueryTexture(acc_desc.Texture, NULL, NULL, &acc_desc.dst.w, &acc_desc.dst.h); 407 | acc_desc.dst.x=time_desc.dst.x+time_desc.dst.w-acc_desc.dst.w; 408 | acc_desc.dst.y=150; 409 | 410 | sprintf(text, "00.00"); 411 | temp_surface=TTF_RenderText_Blended(font,text,color); 412 | acc_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 413 | SDL_QueryTexture(acc_desc_number.Texture, NULL, NULL, &acc_desc_number.dst.w, &acc_desc_number.dst.h); 414 | acc_desc_number.dst.x=acc_desc.dst.x+acc_desc.dst.w+(acc_desc.dst.h/5); 415 | acc_desc_number.dst.y=acc_desc.dst.y; 416 | //END ACC_DESC 417 | 418 | //BEGIN ALT_DESC 419 | sprintf(text, "Alt:"); 420 | temp_surface=TTF_RenderText_Blended(font,text,color); 421 | alt_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 422 | SDL_QueryTexture(alt_desc.Texture, NULL, NULL, &alt_desc.dst.w, &alt_desc.dst.h); 423 | alt_desc.dst.x=time_desc.dst.x+time_desc.dst.w-alt_desc.dst.w; 424 | alt_desc.dst.y=175; 425 | 426 | sprintf(text, "00.00"); 427 | temp_surface=TTF_RenderText_Blended(font,text,color); 428 | alt_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 429 | SDL_QueryTexture(alt_desc_number.Texture, NULL, NULL, &alt_desc_number.dst.w, &alt_desc_number.dst.h); 430 | alt_desc_number.dst.x=alt_desc.dst.x+alt_desc.dst.w+(alt_desc.dst.h/5); 431 | alt_desc_number.dst.y=alt_desc.dst.y; 432 | //END ALT_DESC 433 | 434 | 435 | } 436 | 437 | void assets_out(void) 438 | { 439 | SDL_DestroyTexture(tower_crane.Texture); 440 | SDL_DestroyTexture(sb_desc.Texture); 441 | SDL_DestroyTexture(box.data.Texture); 442 | SDL_DestroyTexture(time_desc.Texture); 443 | SDL_DestroyTexture(time_desc_number.Texture); 444 | SDL_DestroyTexture(acc_desc.Texture); 445 | SDL_DestroyTexture(acc_desc_number.Texture); SDL_DestroyTexture(alt_desc.Texture); 446 | SDL_DestroyTexture(alt_desc_number.Texture); 447 | 448 | TTF_CloseFont(font); 449 | } 450 | 451 | void query_disp (void) 452 | { 453 | int disp; 454 | 455 | disp=SDL_GetWindowDisplayIndex(Window); 456 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 457 | SDL_Log("hdpi: %f", dpi.x); 458 | ww=roundf(SCALESIZE_H / CENTI_PER_INCH * dpi.x); 459 | SDL_Log("vdpi: %f", dpi.y); 460 | wh=roundf(SCALESIZE_V / CENTI_PER_INCH * dpi.y); 461 | SDL_Log("w: %d, h: %d",ww,wh); 462 | SDL_SetWindowSize(Window, ww,wh); 463 | 464 | } 465 | 466 | void earth_gravity(void) 467 | { 468 | // calculating the accelleration with which an object is moving towards 469 | // to the centre of the earth 470 | float earth_mass = 5.9736e+24; //in kg 471 | float earth_radius = 6.375e+6; //in km 472 | float gc = 6.674e-11; //gravitational constant 473 | g=(gc*earth_mass)/(earth_radius*earth_radius); 474 | } 475 | //END ASSETS AND INIT 476 | 477 | //BEGIN UPDATE 478 | void update_desc(float time, float z) 479 | { 480 | char buffer[10]; 481 | sprintf(buffer, "%05.2f", time); 482 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 483 | time_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 484 | SDL_QueryTexture(time_desc_number.Texture, NULL, NULL, &time_desc_number.dst.w, &time_desc_number.dst.h); 485 | 486 | v = g * time; 487 | sprintf(buffer, "%05.2f", v); 488 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 489 | vel_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 490 | SDL_QueryTexture(vel_desc_number.Texture, NULL, NULL, &vel_desc_number.dst.w, &vel_desc_number.dst.h); 491 | 492 | a = v / time; 493 | sprintf(buffer, "%05.2f", a); 494 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 495 | acc_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 496 | SDL_QueryTexture(acc_desc_number.Texture, NULL, NULL, &acc_desc_number.dst.w, &acc_desc_number.dst.h); 497 | 498 | // distance = gravity * powf(time,2) * 0.5; 499 | // z = g * powf(time,2) * 0.5; 500 | sprintf(buffer, "%05.2f", z); 501 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 502 | alt_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 503 | SDL_QueryTexture(alt_desc_number.Texture, NULL, NULL, &alt_desc_number.dst.w, &alt_desc_number.dst.h); 504 | 505 | } 506 | 507 | float time_(void) 508 | { 509 | 510 | static Uint64 start = 0; 511 | static float frequency = 0; 512 | 513 | if (start==0){ 514 | start = SDL_GetPerformanceCounter(); 515 | frequency= (float)SDL_GetPerformanceFrequency(); 516 | return 0.0f; 517 | } 518 | 519 | Uint64 counter = 0; 520 | counter=SDL_GetPerformanceCounter(); 521 | return (((float)counter - (float)start) /frequency); 522 | } 523 | //END UPDATE 524 | 525 | //BEGIN HELPER 526 | void rect_round(struct entity_static *s_entity) 527 | { 528 | s_entity->dst.x=roundf(s_entity->frac.pos.x); 529 | s_entity->dst.y=roundf(s_entity->frac.pos.y); 530 | s_entity->dst.w=roundf(s_entity->frac.size.x); 531 | s_entity->dst.h=roundf(s_entity->frac.size.y); 532 | } 533 | //END HELPER 534 | 535 | //END FUNCTIONS 536 | -------------------------------------------------------------------------------- /src/5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //#include 5 | 6 | // Time 7 | 8 | int main(void) 9 | { 10 | 11 | 12 | float time; 13 | 14 | time = 1.0; 15 | printf("T in seconds : %.f\n",time); 16 | 17 | time = 1e-1; 18 | printf("T in deciseconds : %.1f\n",time); 19 | 20 | time = 1e-2; 21 | printf("T in centiseconds : %.2f\n",time); 22 | 23 | time = 1e-3; 24 | printf("T in milliseconds : %.3f\n",time); 25 | 26 | time = 1e-6; 27 | printf("T in microsecond : %.6f\n",time); 28 | 29 | time = 1e-9; 30 | printf("T in nanoseconds : %.9f\n",time); 31 | 32 | return EXIT_SUCCESS; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/5a.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | 3 | //BEGIN DESCRIPTION 4 | 5 | /* DESCRIPTION: 6 | * Clear the board, meassure Rendering 7 | * Timing precision: float 8 | */ 9 | 10 | /* TODO: 11 | * 12 | * 13 | * 14 | */ 15 | //END DESCRIPTION 16 | 17 | //BEGIN INCLUDES 18 | //system headers 19 | #include 20 | //local headers 21 | #include "helper.h" 22 | //END INCLUDES 23 | 24 | //BEGIN CPP DEFINITIONS 25 | //Some Color Helpers 26 | //R G B 27 | #define RED 255,0,0,255 28 | #define GREEN 0,255,0,255 29 | #define BLUE 0,0,255,255 30 | #define BG 112,170,216,255 31 | //All on 32 | #define WHITE 255,255,255,255 33 | //All off 34 | #define BLACK 0,0,0,255 35 | //END CPP DEFINITIONS 36 | 37 | //BEGIN DATASTRUCTURES 38 | //END DATASTRUCTURES 39 | 40 | //BEGIN GLOBALS 41 | 42 | //BEGIN WINDOW AND SCALING 43 | int ww; 44 | int wh; 45 | 46 | //BEGIN VISIBLES 47 | SDL_Color color={BLUE}; 48 | //END VISIBLES 49 | 50 | //END GLOBALS 51 | 52 | //BEGIN FUNCTION PROTOTYPES 53 | float time_ (void); 54 | void update (float); 55 | //END FUNCTION PROTOTYPES 56 | 57 | //END HEAD 58 | 59 | //BEGIN MAIN FUNCTION 60 | int main(int argc, char *argv[]) 61 | { 62 | 63 | //BEGIN INIT 64 | (void)argc; 65 | (void)argv; 66 | init(); 67 | //BEGIN WINDOW 68 | 69 | 70 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 71 | SDL_SetWindowTitle(Window, "Meassure"); 72 | ww=800; 73 | wh=600; 74 | SDL_SetWindowSize(Window, ww,wh); 75 | SDL_ShowWindow(Window); 76 | 77 | //END WINDOW 78 | float currentTime = 0.0f; 79 | float time_acc = 0.0f; 80 | 81 | SDL_Event event; 82 | int running = 1; 83 | int updates = 0; 84 | int updates_acc = 0; 85 | int pictures = 0; 86 | int pictures_acc = 0; 87 | 88 | float counter = 0; 89 | SDL_SetRenderDrawColor(Renderer, BG); 90 | SDL_RenderClear(Renderer); 91 | //END INIT 92 | 93 | //BEGIN MAIN LOOP 94 | while(running){ 95 | while(SDL_PollEvent(&event)){ 96 | if(event.type == SDL_QUIT){ 97 | running =0; 98 | } 99 | if(event.type == SDL_KEYDOWN ){ 100 | switch(event.key.keysym.sym ){ 101 | case SDLK_ESCAPE: 102 | running =0; 103 | break; 104 | 105 | default: 106 | break; 107 | } 108 | } 109 | } 110 | 111 | 112 | //BEGIN RENDERING 113 | pictures++; 114 | SDL_RenderPresent(Renderer); 115 | //END RENDERING 116 | 117 | float newTime = time_(); 118 | float deltaTime = newTime - currentTime; 119 | currentTime = newTime; 120 | time_acc += deltaTime; 121 | counter += deltaTime; 122 | // update (deltaTime); 123 | updates ++; 124 | if (counter>1.0){ 125 | SDL_Log("counter: %f",counter); 126 | SDL_Log("updates: %d",updates); 127 | SDL_Log("pictures: %d",pictures); 128 | counter = counter - 1.0; 129 | updates_acc += updates; 130 | updates = 0; 131 | pictures_acc += pictures; 132 | pictures = 0; 133 | 134 | } 135 | } 136 | //END MAIN LOOP 137 | SDL_Log("AT: %.2f", time_acc); 138 | SDL_Log("AU: %d", updates_acc); 139 | SDL_Log("AP: %d", pictures_acc); 140 | //BEGIN EXIT 141 | exit_(); 142 | return EXIT_SUCCESS; 143 | //END EXIT 144 | 145 | } 146 | //END MAIN FUNCTION 147 | 148 | //BEGIN FUNCTIONS 149 | 150 | //BEGIN UPDATE 151 | void update(float dt) 152 | { 153 | SDL_Log("DT: %.10f", dt); 154 | } 155 | 156 | float time_(void) 157 | { 158 | 159 | static Uint64 start = 0; 160 | static float frequency = 0; 161 | 162 | if (start==0){ 163 | start = SDL_GetPerformanceCounter(); 164 | frequency= (float)SDL_GetPerformanceFrequency(); 165 | return 0.0f; 166 | } 167 | 168 | Uint64 counter = 0; 169 | counter=SDL_GetPerformanceCounter(); 170 | return (((float)counter - (float)start) /frequency); 171 | } 172 | //END UPDATE 173 | 174 | 175 | //END FUNCTIONS 176 | -------------------------------------------------------------------------------- /src/5b.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | 3 | //BEGIN DESCRIPTION 4 | 5 | /* DESCRIPTION: 6 | * Timing precision: double 7 | */ 8 | 9 | /* TODO: 10 | * 11 | * 12 | * 13 | */ 14 | //END DESCRIPTION 15 | 16 | //BEGIN INCLUDES 17 | //system headers 18 | #include 19 | //local headers 20 | #include "helper.h" 21 | //END INCLUDES 22 | 23 | //BEGIN CPP DEFINITIONS 24 | //Some Color Helpers 25 | //R G B 26 | #define RED 255,0,0,255 27 | #define GREEN 0,255,0,255 28 | #define BLUE 0,0,255,255 29 | #define BG 112,170,216,255 30 | //All on 31 | #define WHITE 255,255,255,255 32 | //All off 33 | #define BLACK 0,0,0,255 34 | //END CPP DEFINITIONS 35 | 36 | //BEGIN DATASTRUCTURES 37 | //END DATASTRUCTURES 38 | 39 | //BEGIN GLOBALS 40 | 41 | //BEGIN WINDOW AND SCALING 42 | int ww; 43 | int wh; 44 | 45 | //BEGIN VISIBLES 46 | SDL_Color color={BLUE}; 47 | //END VISIBLES 48 | 49 | //END GLOBALS 50 | 51 | //BEGIN FUNCTION PROTOTYPES 52 | double time_ (void); 53 | void update (double); 54 | //END FUNCTION PROTOTYPES 55 | 56 | //END HEAD 57 | 58 | //BEGIN MAIN FUNCTION 59 | int main(int argc, char *argv[]) 60 | { 61 | 62 | //BEGIN INIT 63 | (void)argc; 64 | (void)argv; 65 | init(); 66 | //BEGIN WINDOW 67 | 68 | 69 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 70 | SDL_SetWindowTitle(Window, "Meassure"); 71 | ww=800; 72 | wh=600; 73 | SDL_SetWindowSize(Window, ww,wh); 74 | SDL_ShowWindow(Window); 75 | 76 | //END WINDOW 77 | double currentTime = 0.0f; 78 | double time_acc = 0.0f; 79 | 80 | SDL_Event event; 81 | int running = 1; 82 | int updates = 0; 83 | int updates_acc = 0; 84 | int pictures = 0; 85 | int pictures_acc = 0; 86 | 87 | double counter = 0; 88 | SDL_SetRenderDrawColor(Renderer, BG); 89 | SDL_RenderClear(Renderer); 90 | //END INIT 91 | 92 | //BEGIN MAIN LOOP 93 | while(running){ 94 | while(SDL_PollEvent(&event)){ 95 | if(event.type == SDL_QUIT){ 96 | running =0; 97 | } 98 | if(event.type == SDL_KEYDOWN ){ 99 | switch(event.key.keysym.sym ){ 100 | case SDLK_ESCAPE: 101 | running =0; 102 | break; 103 | 104 | default: 105 | break; 106 | } 107 | } 108 | } 109 | 110 | 111 | //BEGIN RENDERING 112 | pictures++; 113 | SDL_RenderPresent(Renderer); 114 | //END RENDERING 115 | 116 | double newTime = time_(); 117 | double deltaTime = newTime - currentTime; 118 | currentTime = newTime; 119 | time_acc += deltaTime; 120 | counter += deltaTime; 121 | // update (deltaTime); 122 | updates ++; 123 | if (counter>1.0){ 124 | SDL_Log("counter: %f",counter); 125 | SDL_Log("updates: %d",updates); 126 | SDL_Log("pictures: %d",pictures); 127 | counter = counter - 1.0; 128 | updates_acc += updates; 129 | updates = 0; 130 | pictures_acc += pictures; 131 | pictures = 0; 132 | 133 | } 134 | } 135 | //END MAIN LOOP 136 | SDL_Log("AT: %.2f", time_acc); 137 | SDL_Log("AU: %d", updates_acc); 138 | SDL_Log("AP: %d", pictures_acc); 139 | //BEGIN EXIT 140 | exit_(); 141 | return EXIT_SUCCESS; 142 | //END EXIT 143 | 144 | } 145 | //END MAIN FUNCTION 146 | 147 | //BEGIN FUNCTIONS 148 | 149 | //BEGIN UPDATE 150 | void update(double dt) 151 | { 152 | SDL_Log("DT: %.10f", dt); 153 | } 154 | 155 | double time_(void) 156 | { 157 | 158 | static Uint64 start = 0; 159 | static double frequency = 0; 160 | 161 | if (start==0){ 162 | start = SDL_GetPerformanceCounter(); 163 | frequency= (double)SDL_GetPerformanceFrequency(); 164 | return 0.0f; 165 | } 166 | 167 | Uint64 counter = 0; 168 | counter=SDL_GetPerformanceCounter(); 169 | return (((double)counter - (double)start) /frequency); 170 | } 171 | //END UPDATE 172 | 173 | 174 | //END FUNCTIONS 175 | -------------------------------------------------------------------------------- /src/5c.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | 3 | //BEGIN DESCRIPTION 4 | 5 | /* DESCRIPTION: 6 | * Timing precision: long double 7 | */ 8 | 9 | /* TODO: 10 | * 11 | * 12 | * 13 | */ 14 | //END DESCRIPTION 15 | 16 | //BEGIN INCLUDES 17 | //system headers 18 | #include 19 | //local headers 20 | #include "helper.h" 21 | //END INCLUDES 22 | 23 | //BEGIN CPP DEFINITIONS 24 | //Some Color Helpers 25 | //R G B 26 | #define RED 255,0,0,255 27 | #define GREEN 0,255,0,255 28 | #define BLUE 0,0,255,255 29 | #define BG 112,170,216,255 30 | //All on 31 | #define WHITE 255,255,255,255 32 | //All off 33 | #define BLACK 0,0,0,255 34 | //END CPP DEFINITIONS 35 | 36 | //BEGIN DATASTRUCTURES 37 | //END DATASTRUCTURES 38 | 39 | //BEGIN GLOBALS 40 | 41 | //BEGIN WINDOW AND SCALING 42 | int ww; 43 | int wh; 44 | 45 | //BEGIN VISIBLES 46 | SDL_Color color={BLUE}; 47 | //END VISIBLES 48 | 49 | //END GLOBALS 50 | 51 | //BEGIN FUNCTION PROTOTYPES 52 | long double time_ (void); 53 | void update (long double); 54 | //END FUNCTION PROTOTYPES 55 | 56 | //END HEAD 57 | 58 | //BEGIN MAIN FUNCTION 59 | int main(int argc, char *argv[]) 60 | { 61 | 62 | //BEGIN INIT 63 | (void)argc; 64 | (void)argv; 65 | init(); 66 | //BEGIN WINDOW 67 | 68 | 69 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 70 | SDL_SetWindowTitle(Window, "Meassure"); 71 | ww=800; 72 | wh=600; 73 | SDL_SetWindowSize(Window, ww,wh); 74 | SDL_ShowWindow(Window); 75 | 76 | //END WINDOW 77 | long double currentTime = 0.0f; 78 | long double time_acc = 0.0f; 79 | 80 | SDL_Event event; 81 | int running = 1; 82 | int updates = 0; 83 | int updates_acc = 0; 84 | int pictures = 0; 85 | int pictures_acc = 0; 86 | 87 | long double counter = 0; 88 | SDL_SetRenderDrawColor(Renderer, BG); 89 | SDL_RenderClear(Renderer); 90 | //END INIT 91 | 92 | //BEGIN MAIN LOOP 93 | while(running){ 94 | while(SDL_PollEvent(&event)){ 95 | if(event.type == SDL_QUIT){ 96 | running =0; 97 | } 98 | if(event.type == SDL_KEYDOWN ){ 99 | switch(event.key.keysym.sym ){ 100 | case SDLK_ESCAPE: 101 | running =0; 102 | break; 103 | 104 | default: 105 | break; 106 | } 107 | } 108 | } 109 | 110 | 111 | //BEGIN RENDERING 112 | pictures++; 113 | SDL_RenderPresent(Renderer); 114 | //END RENDERING 115 | 116 | long double newTime = time_(); 117 | long double deltaTime = newTime - currentTime; 118 | currentTime = newTime; 119 | time_acc += deltaTime; 120 | counter += deltaTime; 121 | update (deltaTime); 122 | updates ++; 123 | if (counter>1.0){ 124 | SDL_Log("counter: %Lf",counter); 125 | SDL_Log("updates: %d",updates); 126 | SDL_Log("pictures: %d",pictures); 127 | counter = counter - 1.0; 128 | updates_acc += updates; 129 | updates = 0; 130 | pictures_acc += pictures; 131 | pictures = 0; 132 | 133 | } 134 | } 135 | //END MAIN LOOP 136 | SDL_Log("AT: %.2Lf", time_acc); 137 | SDL_Log("AU: %d", updates_acc); 138 | SDL_Log("AP: %d", pictures_acc); 139 | //BEGIN EXIT 140 | exit_(); 141 | return EXIT_SUCCESS; 142 | //END EXIT 143 | 144 | } 145 | //END MAIN FUNCTION 146 | 147 | //BEGIN FUNCTIONS 148 | 149 | //BEGIN UPDATE 150 | void update(long double dt) 151 | { 152 | SDL_Log("DT: %.10Lf", dt); 153 | } 154 | 155 | long double time_(void) 156 | { 157 | 158 | static Uint64 start = 0; 159 | static long double frequency = 0; 160 | 161 | if (start==0){ 162 | start = SDL_GetPerformanceCounter(); 163 | frequency= (long double)SDL_GetPerformanceFrequency(); 164 | return 0.0f; 165 | } 166 | 167 | Uint64 counter = 0; 168 | counter=SDL_GetPerformanceCounter(); 169 | return (((long double)counter - (long double)start) /frequency); 170 | } 171 | //END UPDATE 172 | 173 | 174 | //END FUNCTIONS 175 | -------------------------------------------------------------------------------- /src/5d.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | 3 | //BEGIN DESCRIPTION 4 | 5 | /* DESCRIPTION: 6 | * Timing precision: Uint64 7 | // SDL_GetPerformanceFrequency 8 | // Use this function to get the count per second of the high resolution counter. 9 | */ 10 | 11 | /* 12 | 10^-1 s ds decisecond 0.1 13 | 10^-2 s cs centisecond 0.01 14 | 10^-3 s ms millisecond 0.001 15 | 10^-6 s µs microsecond 0.000001 16 | 10^-9 s ns nanosecond 0.000000001 17 | */ 18 | 19 | /* TODO: 20 | * 21 | * 22 | * 23 | */ 24 | //END DESCRIPTION 25 | 26 | //BEGIN INCLUDES 27 | //system headers 28 | #include 29 | //local headers 30 | #include "helper.h" 31 | //END INCLUDES 32 | 33 | //BEGIN CPP DEFINITIONS 34 | //Some Color Helpers 35 | //R G B 36 | #define RED 255,0,0,255 37 | #define GREEN 0,255,0,255 38 | #define BLUE 0,0,255,255 39 | #define BG 112,170,216,255 40 | //All on 41 | #define WHITE 255,255,255,255 42 | //All off 43 | #define BLACK 0,0,0,255 44 | //END CPP DEFINITIONS 45 | 46 | //BEGIN DATASTRUCTURES 47 | //END DATASTRUCTURES 48 | 49 | //BEGIN GLOBALS 50 | 51 | //BEGIN WINDOW AND SCALING 52 | int ww; 53 | int wh; 54 | 55 | //BEGIN VISIBLES 56 | SDL_Color color={BLUE}; 57 | //END VISIBLES 58 | 59 | //END GLOBALS 60 | 61 | //BEGIN FUNCTION PROTOTYPES 62 | Uint64 itime (void); 63 | long double ftime (void); 64 | void update (Uint64); 65 | //END FUNCTION PROTOTYPES 66 | 67 | //END HEAD 68 | 69 | //BEGIN MAIN FUNCTION 70 | int main(int argc, char *argv[]) 71 | { 72 | 73 | //BEGIN INIT 74 | (void)argc; 75 | (void)argv; 76 | init(); 77 | //BEGIN WINDOW 78 | 79 | 80 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 81 | SDL_SetWindowTitle(Window, "Meassure"); 82 | ww=800; 83 | wh=600; 84 | SDL_SetWindowSize(Window, ww,wh); 85 | SDL_ShowWindow(Window); 86 | 87 | //END WINDOW 88 | 89 | SDL_Event event; 90 | int running = 1; 91 | int updates = 0; 92 | int updates_acc = 0; 93 | int pictures = 0; 94 | int pictures_acc = 0; 95 | 96 | SDL_SetRenderDrawColor(Renderer, BG); 97 | SDL_RenderClear(Renderer); 98 | //END INIT 99 | 100 | Uint64 currentTime = 0; 101 | Uint64 time_acc = 0; 102 | Uint64 counter = 0; 103 | Uint64 newTime = 0; 104 | Uint64 deltaTime = 0; 105 | Uint64 frequency = SDL_GetPerformanceFrequency(); 106 | char dt = 0; 107 | SDL_Log("Freq: %lu", frequency); 108 | // exit_(); 109 | // return EXIT_SUCCESS; 110 | //BEGIN MAIN LOOP 111 | while(running){ 112 | while(SDL_PollEvent(&event)){ 113 | if(event.type == SDL_QUIT){ 114 | running =0; 115 | } 116 | if(event.type == SDL_KEYDOWN ){ 117 | switch(event.key.keysym.sym ){ 118 | case SDLK_ESCAPE: 119 | running =0; 120 | break; 121 | case SDLK_d: 122 | dt=dt^1; //XOR - flip var 123 | default: 124 | break; 125 | } 126 | } 127 | } 128 | 129 | 130 | //BEGIN RENDERING 131 | pictures++; 132 | SDL_RenderPresent(Renderer); 133 | //END RENDERING 134 | 135 | newTime = itime(); 136 | deltaTime = newTime - currentTime; 137 | currentTime = newTime; 138 | time_acc += deltaTime; 139 | counter += deltaTime; 140 | if (dt) 141 | update (deltaTime); 142 | updates ++; 143 | if (counter>frequency){ 144 | SDL_Log("counter: %lu",counter); 145 | SDL_Log("updates/s: %d",updates); 146 | SDL_Log("pictures/s: %d",pictures); 147 | counter = counter - frequency; 148 | updates_acc += updates; 149 | updates = 0; 150 | pictures_acc += pictures; 151 | pictures = 0; 152 | 153 | } 154 | } 155 | //END MAIN LOOP 156 | Uint64 seconds = time_acc/frequency; 157 | Uint64 rest = time_acc - (seconds*frequency); 158 | SDL_Log("AT: %lu", time_acc); 159 | SDL_Log("Rest: %lu", rest); 160 | SDL_Log("AiT: %lu.%lu", seconds,rest); 161 | SDL_Log("AfT: %f", (float)time_acc/(float)frequency); 162 | SDL_Log("AU: %d", updates_acc); 163 | SDL_Log("AP: %d", pictures_acc); 164 | //BEGIN EXIT 165 | exit_(); 166 | return EXIT_SUCCESS; 167 | //END EXIT 168 | 169 | } 170 | //END MAIN FUNCTION 171 | 172 | //BEGIN FUNCTIONS 173 | 174 | //BEGIN UPDATE 175 | void update(Uint64 dt) 176 | { 177 | SDL_Log("DT in ns: %lu", dt); 178 | SDL_Log("DT in ms: %lu", dt/(1000*1000) ); 179 | } 180 | 181 | Uint64 itime(void) 182 | { 183 | 184 | static Uint64 start = 0; 185 | if (start==0){ 186 | start = SDL_GetPerformanceCounter(); 187 | return 0; 188 | } 189 | Uint64 counter = 0; 190 | counter=SDL_GetPerformanceCounter(); 191 | return counter - start; 192 | } 193 | 194 | long double ftime(void) 195 | { 196 | 197 | static Uint64 start = 0; 198 | static long double frequency = 0; 199 | 200 | if (start==0){ 201 | start = SDL_GetPerformanceCounter(); 202 | frequency= (long double)SDL_GetPerformanceFrequency(); 203 | return 0.0f; 204 | } 205 | 206 | Uint64 counter = 0; 207 | counter=SDL_GetPerformanceCounter(); 208 | return (((long double)counter - (long double)start) /frequency); 209 | } 210 | //END UPDATE 211 | 212 | 213 | //END FUNCTIONS 214 | -------------------------------------------------------------------------------- /src/6.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | 3 | /* DESCRIPTION: 4 | */ 5 | 6 | 7 | //BEGIN INCLUDES 8 | //system headers 9 | #include 10 | //local headers 11 | #include "helper.h" 12 | //END INCLUDES 13 | 14 | //BEGIN CPP DEFINITIONS 15 | //Some Color Helpers 16 | //R G B 17 | #define RED 255,0,0,255 18 | #define GREEN 0,255,0,255 19 | #define BLUE 0,0,255,255 20 | #define BG 112,170,216,255 21 | //All on 22 | #define WHITE 255,255,255,255 23 | //All off 24 | #define BLACK 0,0,0,255 25 | 26 | //Real Size in SI-Unit meters 27 | #define REALSIZE_H 82.1305 28 | #define REALSIZE_V 52.8554 29 | 30 | #define SCALE_RATIO 4.0 31 | 32 | //Real Size in SI-Unit centimeters 33 | #define SCALE_UNITS 100.0 34 | 35 | #define SCALESIZE_H REALSIZE_H/SCALE_RATIO 36 | #define SCALESIZE_V REALSIZE_V/SCALE_RATIO 37 | 38 | //DPI is Dots per Inch - Industry should fix that 39 | #define CENTI_PER_INCH 2.54 40 | //END CPP DEFINITIONS 41 | 42 | //BEGIN DATASTRUCTURES 43 | struct vec2{ 44 | float x; 45 | float y; 46 | }; 47 | 48 | struct rect{ 49 | struct vec2 pos; 50 | struct vec2 size; 51 | }; 52 | 53 | struct entity_static{ 54 | SDL_Texture *Texture; 55 | struct rect frac; 56 | SDL_Rect dst; 57 | }; 58 | 59 | struct entity_dyn{ 60 | struct entity_static data; 61 | struct vec2 vel; 62 | }; 63 | //END DATASTRUCTURES 64 | 65 | //BEGIN GLOBALS 66 | 67 | //BEGIN WINDOW AND SCALING 68 | int ww; 69 | int wh; 70 | 71 | int IARH; // Image Aspect Ratio horizontal 72 | int IARW; // Image Aspect Ratio vertical 73 | 74 | struct vec2 dpi; 75 | 76 | float screen_centimeter_x; 77 | float screen_centimeter_y; 78 | 79 | float real_meter_x; 80 | float real_meter_y; 81 | //END WINDOW AND SCALING 82 | 83 | 84 | float g; // Gravity 85 | float v; // Velocity 86 | float a; // Acceleration 87 | float z; // Height 88 | 89 | float distance_m; 90 | float distance_p; 91 | 92 | float Altitude; 93 | SDL_Event event; 94 | int running = 1; 95 | char pause = 1; 96 | char set = 1; 97 | char log_dt = 0; 98 | Uint64 time_acc = 0; 99 | Uint64 frequency; 100 | //BEGIN VISIBLES 101 | SDL_Surface *temp_surface = NULL; 102 | SDL_Color color={BLACK}; 103 | TTF_Font *font = NULL; 104 | struct entity_static sb_desc; 105 | struct entity_static sbr[8]; 106 | struct entity_static tower_crane; 107 | struct entity_dyn box; 108 | struct entity_static time_desc; 109 | struct entity_static time_desc_number; 110 | struct entity_static vel_desc; 111 | struct entity_static vel_desc_number; 112 | struct entity_static acc_desc; 113 | struct entity_static acc_desc_number; 114 | struct entity_static alt_desc; 115 | struct entity_static alt_desc_number; 116 | //END VISIBLES 117 | 118 | //END GLOBALS 119 | 120 | //BEGIN FUNCTION PROTOTYPES 121 | void query_disp (void); 122 | void assets_in (void); 123 | void assets_out (void); 124 | 125 | Uint64 itime (void); 126 | void update (Uint64, Uint64); 127 | void update_desc (Uint64, float); 128 | void get_events (void); 129 | void draw (void); 130 | void rect_round (struct entity_static *s_entity); 131 | void earth_gravity (void); 132 | //END FUNCTION PROTOTYPES 133 | 134 | //END HEAD 135 | 136 | //BEGIN MAIN FUNCTION 137 | int main(int argc, char *argv[]) 138 | { 139 | 140 | //BEGIN INIT 141 | (void)argc; 142 | (void)argv; 143 | init(); 144 | query_disp(); 145 | earth_gravity(); 146 | SDL_Log("Gravity: %.2f m/s²", g); 147 | assets_in(); 148 | 149 | //BEGIN WINDOW 150 | char title[20]; 151 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 152 | sprintf(title, "%.2f x %.2f cm",SCALESIZE_H,SCALESIZE_V); 153 | SDL_SetWindowTitle(Window, title); 154 | SDL_ShowWindow(Window); 155 | //END WINDOW 156 | 157 | Uint64 currentTime = 0; 158 | 159 | Uint64 counter = 0; 160 | Uint64 newTime = 0; 161 | Uint64 deltaTime = 0; 162 | frequency = SDL_GetPerformanceFrequency(); 163 | z = distance_m; 164 | 165 | //END INIT 166 | 167 | //BEGIN MAIN LOOP 168 | while(running){ 169 | get_events(); 170 | newTime = itime(); 171 | deltaTime = newTime - currentTime; 172 | currentTime = newTime; 173 | 174 | if (set && !pause){ 175 | time_acc += deltaTime; 176 | counter += deltaTime; 177 | update (time_acc, deltaTime); 178 | } 179 | draw(); 180 | } 181 | //END MAIN LOOP 182 | 183 | //BEGIN EXIT 184 | assets_out(); 185 | exit_(); 186 | return EXIT_SUCCESS; 187 | //END EXIT 188 | 189 | } 190 | //END MAIN FUNCTION 191 | 192 | //BEGIN FUNCTIONS 193 | //BEGIN UPDATE 194 | void update(Uint64 acc, Uint64 dt) 195 | { 196 | if (log_dt){ 197 | Uint64 ms = dt/1000/1000; 198 | Uint64 rest = dt - (ms*1000*1000); 199 | // SDL_Log("DT: %lu", ms); 200 | // SDL_Log("Rest: %lu", rest); 201 | SDL_Log("DT in ms: %lu.%lu", ms,rest); 202 | } 203 | 204 | 205 | // velocity += timestep * acceleration; 206 | SDL_Log("G: %f",g); 207 | SDL_Log("dt: %lu",dt); 208 | // float f_ms = dt/1000/1000; 209 | float f_s = (float)dt / (float)frequency; 210 | SDL_Log("dt: %f", f_s); 211 | 212 | 213 | // position += timestep * velocity; 214 | v += g * f_s; 215 | SDL_Log("v: %f",v); 216 | z -= f_s * v; 217 | SDL_Log("z: %f",z); 218 | box.data.frac.pos.y = (float)wh-(real_meter_y*z)-box.data.frac.size.y; 219 | // box.data.frac.pos.y += f_s * v; 220 | 221 | /* 222 | if (Altitude > 0){ 223 | Altitude = distance_m - 0.5 * g * acc/frequency * acc/frequency; 224 | 225 | box.data.frac.pos.y = (float)wh-(real_meter_y*Altitude)-box.data.frac.size.y; 226 | box.data.dst.y = ceilf(box.data.frac.pos.y); 227 | } 228 | */ 229 | 230 | if (box.data.frac.pos.y >= (float)wh - box.data.frac.size.y){ 231 | //box.data.frac.pos.y = (float)wh - box.data.frac.size.y; 232 | 233 | set = 0; 234 | // Altitude = 0; 235 | } 236 | 237 | box.data.dst.y = ceilf(box.data.frac.pos.y); 238 | update_desc (acc, z); 239 | } 240 | 241 | void update_desc(Uint64 time_acc, float z) 242 | { 243 | float time = (float)time_acc/(float)frequency; 244 | char buffer[10]; 245 | sprintf(buffer, "%05.2f", time); 246 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 247 | time_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 248 | SDL_QueryTexture(time_desc_number.Texture, NULL, NULL, &time_desc_number.dst.w, &time_desc_number.dst.h); 249 | 250 | // v = g * time; 251 | sprintf(buffer, "%05.2f", v); 252 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 253 | vel_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 254 | SDL_QueryTexture(vel_desc_number.Texture, NULL, NULL, &vel_desc_number.dst.w, &vel_desc_number.dst.h); 255 | 256 | a = v / time; 257 | sprintf(buffer, "%05.2f", a); 258 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 259 | acc_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 260 | SDL_QueryTexture(acc_desc_number.Texture, NULL, NULL, &acc_desc_number.dst.w, &acc_desc_number.dst.h); 261 | 262 | // distance = gravity * powf(time,2) * 0.5; 263 | // z = g * powf(time,2) * 0.5; 264 | // float h = distance_m -z; 265 | sprintf(buffer, "%05.2f",z); 266 | temp_surface=TTF_RenderText_Blended(font,buffer,color); 267 | alt_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 268 | SDL_QueryTexture(alt_desc_number.Texture, NULL, NULL, &alt_desc_number.dst.w, &alt_desc_number.dst.h); 269 | 270 | } 271 | 272 | Uint64 itime(void) 273 | { 274 | 275 | static Uint64 start = 0; 276 | if (start==0){ 277 | start = SDL_GetPerformanceCounter(); 278 | return 0; 279 | } 280 | Uint64 counter = 0; 281 | counter=SDL_GetPerformanceCounter(); 282 | return counter - start; 283 | } 284 | //END UPDATE 285 | 286 | //BEGIN ASSETS AND INIT 287 | void assets_in(void) 288 | { 289 | //BEGIN TOWER CRANE 290 | temp_surface = IMG_Load("./assets/gfx/tower_crane.png"); 291 | tower_crane.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 292 | tower_crane.frac.size.y = wh; 293 | tower_crane.frac.size.x = ww; 294 | tower_crane.frac.pos.y = 0; 295 | tower_crane.frac.pos.x = 0; 296 | rect_round(&tower_crane); 297 | //END TOWER_CRANE 298 | 299 | //BEGIN SCALING 300 | //How many pixels are one centimeter on the screen? 301 | screen_centimeter_x = (SCALESIZE_H * SCALE_RATIO / CENTI_PER_INCH * dpi.x) / REALSIZE_H; 302 | SDL_Log("pixels are one centimeter on the picture x: %f", screen_centimeter_x); 303 | screen_centimeter_y = (SCALESIZE_V * SCALE_RATIO / CENTI_PER_INCH * dpi.y) / REALSIZE_V; 304 | SDL_Log("pixels are one centimeter on the picture y: %f", screen_centimeter_y); 305 | 306 | //How many pixels are one real meter? 307 | float screen_size_x=SCALESIZE_H / CENTI_PER_INCH * dpi.x; //it is ww, just repeated for precision 308 | float screen_size_y=SCALESIZE_V / CENTI_PER_INCH * dpi.y; //it is wh, just repeated for precision 309 | 310 | SDL_Log("screen_size_y: %f", screen_size_y); 311 | 312 | real_meter_x=screen_size_x/REALSIZE_H; 313 | SDL_Log("real_meter_x in pixels: %f", real_meter_x); 314 | 315 | real_meter_y=screen_size_y/REALSIZE_V; 316 | SDL_Log("real_meter_y in pixels: %f", real_meter_y); 317 | //END SCALING 318 | 319 | //BEGIN TESTBOX 320 | temp_surface = IMG_Load("./assets/gfx/container.png"); 321 | box.data.Texture=SDL_CreateTextureFromSurface(Renderer, temp_surface); 322 | box.data.frac.size.y = real_meter_x*2.5; 323 | box.data.frac.size.x = real_meter_y*8; 324 | box.data.frac.pos.x = real_meter_x*67.5; 325 | box.data.frac.pos.y = real_meter_y*12.3; 326 | rect_round(&box.data); 327 | box.vel.y = 0; 328 | box.vel.x = 0; 329 | //END TESTBOX 330 | 331 | //BEGIN SCALING2 332 | //falling distance in real meters 333 | distance_m=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y)/real_meter_y; 334 | SDL_Log("falling distance: %.2f m.", distance_m); 335 | 336 | distance_p=(screen_size_y-box.data.frac.pos.y-box.data.frac.size.y); 337 | SDL_Log("falling distance in pixel: %f", distance_p); 338 | 339 | // These are the expected results for our values: 340 | float fall_time = sqrtf(2 * distance_m / g); 341 | SDL_Log("fall time: %f seconds", fall_time); 342 | // t 3,0142 s 343 | // v 29,5596 m/s 344 | // v 106,4147 km/h 345 | //END SCALING2 346 | 347 | //BEGIN SB_DESC 348 | font=TTF_OpenFont("./assets/fonts/FiraCode-Regular.ttf", 16); 349 | char text[30]; 350 | sprintf(text, "Map scale = 1: %.0f cm",SCALE_RATIO*SCALE_UNITS); 351 | temp_surface=TTF_RenderText_Blended(font,text,color); 352 | sb_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 353 | SDL_QueryTexture(sb_desc.Texture, NULL, NULL, &sb_desc.dst.w, &sb_desc.dst.h); 354 | sb_desc.dst.x=ww-sb_desc.dst.w-50; 355 | sb_desc.dst.y=25; 356 | //END SB_DESC 357 | 358 | //BEGIN SCALE BAR RECT 359 | sbr[0].dst.w = roundf(0.5 / CENTI_PER_INCH*dpi.x); 360 | sbr[0].dst.h = sbr[0].dst.w/3; 361 | // sbr[0].dst.x = sbr[0].dst.w*2; 362 | sbr[0].dst.x = sb_desc.dst.x; 363 | // sbr[0].dst.y = sbr[0].dst.w*4; 364 | sbr[0].dst.y = sb_desc.dst.y + sb_desc.dst.h + sb_desc.dst.h / 5; 365 | 366 | for (int i=1; i<8; i++){ 367 | sbr[i].dst.w=sbr[0].dst.w; 368 | sbr[i].dst.h=sbr[0].dst.h; 369 | sbr[i].dst.y=sbr[0].dst.y; 370 | 371 | sbr[i].dst.x=sbr[0].dst.x + (sbr[0].dst.w*i); 372 | } 373 | //END SCALE BAR RECT 374 | 375 | //BEGIN TIME_DESC 376 | sprintf(text, "Accumulated Time:"); 377 | temp_surface=TTF_RenderText_Blended(font,text,color); 378 | time_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 379 | SDL_QueryTexture(time_desc.Texture, NULL, NULL, &time_desc.dst.w, &time_desc.dst.h); 380 | time_desc.dst.x=ww/3; 381 | time_desc.dst.y=100; 382 | 383 | sprintf(text, "00.00"); 384 | temp_surface=TTF_RenderText_Blended(font,text,color); 385 | time_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 386 | SDL_QueryTexture(time_desc_number.Texture, NULL, NULL, &time_desc_number.dst.w, &time_desc_number.dst.h); 387 | time_desc_number.dst.x=time_desc.dst.x+time_desc.dst.w+(time_desc.dst.h/5); 388 | time_desc_number.dst.y=time_desc.dst.y; 389 | //END TIME_DESC 390 | 391 | //BEGIN VEL_DESC 392 | sprintf(text, "Vel in m/s:"); 393 | temp_surface=TTF_RenderText_Blended(font,text,color); 394 | vel_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 395 | SDL_QueryTexture(vel_desc.Texture, NULL, NULL, &vel_desc.dst.w, &vel_desc.dst.h); 396 | vel_desc.dst.x=time_desc.dst.x+time_desc.dst.w-vel_desc.dst.w; 397 | vel_desc.dst.y=125; 398 | 399 | sprintf(text, "00.00"); 400 | temp_surface=TTF_RenderText_Blended(font,text,color); 401 | vel_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 402 | SDL_QueryTexture(vel_desc_number.Texture, NULL, NULL, &vel_desc_number.dst.w, &vel_desc_number.dst.h); 403 | vel_desc_number.dst.x=vel_desc.dst.x+vel_desc.dst.w+(vel_desc.dst.h/5); 404 | vel_desc_number.dst.y=vel_desc.dst.y; 405 | //END VEL_DESC 406 | 407 | //BEGIN ACC_DESC 408 | sprintf(text, "Acc:"); 409 | temp_surface=TTF_RenderText_Blended(font,text,color); 410 | acc_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 411 | SDL_QueryTexture(acc_desc.Texture, NULL, NULL, &acc_desc.dst.w, &acc_desc.dst.h); 412 | acc_desc.dst.x=time_desc.dst.x+time_desc.dst.w-acc_desc.dst.w; 413 | acc_desc.dst.y=150; 414 | 415 | sprintf(text, "00.00"); 416 | temp_surface=TTF_RenderText_Blended(font,text,color); 417 | acc_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 418 | SDL_QueryTexture(acc_desc_number.Texture, NULL, NULL, &acc_desc_number.dst.w, &acc_desc_number.dst.h); 419 | acc_desc_number.dst.x=acc_desc.dst.x+acc_desc.dst.w+(acc_desc.dst.h/5); 420 | acc_desc_number.dst.y=acc_desc.dst.y; 421 | //END ACC_DESC 422 | 423 | //BEGIN ALT_DESC 424 | sprintf(text, "Alt:"); 425 | temp_surface=TTF_RenderText_Blended(font,text,color); 426 | alt_desc.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 427 | SDL_QueryTexture(alt_desc.Texture, NULL, NULL, &alt_desc.dst.w, &alt_desc.dst.h); 428 | alt_desc.dst.x=time_desc.dst.x+time_desc.dst.w-alt_desc.dst.w; 429 | alt_desc.dst.y=175; 430 | 431 | sprintf(text, "00.00"); 432 | temp_surface=TTF_RenderText_Blended(font,text,color); 433 | alt_desc_number.Texture = SDL_CreateTextureFromSurface(Renderer, temp_surface); 434 | SDL_QueryTexture(alt_desc_number.Texture, NULL, NULL, &alt_desc_number.dst.w, &alt_desc_number.dst.h); 435 | alt_desc_number.dst.x=alt_desc.dst.x+alt_desc.dst.w+(alt_desc.dst.h/5); 436 | alt_desc_number.dst.y=alt_desc.dst.y; 437 | //END ALT_DESC 438 | 439 | } 440 | 441 | void assets_out(void) 442 | { 443 | SDL_DestroyTexture(tower_crane.Texture); 444 | SDL_DestroyTexture(sb_desc.Texture); 445 | SDL_DestroyTexture(box.data.Texture); 446 | SDL_DestroyTexture(time_desc.Texture); 447 | SDL_DestroyTexture(time_desc_number.Texture); 448 | SDL_DestroyTexture(acc_desc.Texture); 449 | SDL_DestroyTexture(acc_desc_number.Texture); SDL_DestroyTexture(alt_desc.Texture); 450 | SDL_DestroyTexture(alt_desc_number.Texture); 451 | 452 | TTF_CloseFont(font); 453 | } 454 | 455 | void query_disp (void) 456 | { 457 | int disp; 458 | 459 | disp=SDL_GetWindowDisplayIndex(Window); 460 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 461 | SDL_Log("hdpi: %f", dpi.x); 462 | ww=roundf(SCALESIZE_H / CENTI_PER_INCH * dpi.x); 463 | SDL_Log("vdpi: %f", dpi.y); 464 | wh=roundf(SCALESIZE_V / CENTI_PER_INCH * dpi.y); 465 | SDL_Log("w: %d, h: %d",ww,wh); 466 | SDL_SetWindowSize(Window, ww,wh); 467 | 468 | } 469 | 470 | void earth_gravity(void) 471 | { 472 | // calculating the accelleration with which an object is moving towards 473 | // to the centre of the earth 474 | float earth_mass = 5.9736e+24; //in kg 475 | float earth_radius = 6.375e+6; //in km 476 | float gc = 6.674e-11; //gravitational constant 477 | g=(gc*earth_mass)/(earth_radius*earth_radius); 478 | } 479 | //END ASSETS AND INIT 480 | 481 | 482 | 483 | void draw(void) 484 | { 485 | //BEGIN RENDERING 486 | SDL_SetRenderDrawColor(Renderer, BG); 487 | SDL_RenderClear(Renderer); 488 | SDL_RenderCopy(Renderer, tower_crane.Texture, NULL, &tower_crane.dst); 489 | SDL_RenderCopy(Renderer, box.data.Texture, NULL, &box.data.dst); 490 | SDL_RenderCopy(Renderer, sb_desc.Texture, NULL, &sb_desc.dst); 491 | //BEGIN SCALING BAR 492 | for (int i=0; i<8; i++){ 493 | if (i%2){ 494 | SDL_SetRenderDrawColor(Renderer, WHITE); 495 | SDL_RenderFillRect(Renderer, &sbr[i].dst); 496 | SDL_SetRenderDrawColor(Renderer, BLACK); 497 | SDL_RenderDrawRect(Renderer, &sbr[i].dst); 498 | }else{ 499 | SDL_SetRenderDrawColor(Renderer, BLACK); 500 | SDL_RenderFillRect(Renderer, &sbr[i].dst); 501 | } 502 | } 503 | //END SCALING BAR 504 | SDL_RenderCopy(Renderer, time_desc.Texture, NULL, &time_desc.dst); 505 | SDL_RenderCopy(Renderer, time_desc_number.Texture, NULL, &time_desc_number.dst); 506 | SDL_RenderCopy(Renderer, vel_desc.Texture, NULL, &vel_desc.dst); 507 | SDL_RenderCopy(Renderer, vel_desc_number.Texture, NULL, &vel_desc_number.dst); 508 | SDL_RenderCopy(Renderer, acc_desc.Texture, NULL, &acc_desc.dst); 509 | SDL_RenderCopy(Renderer, acc_desc_number.Texture, NULL, &acc_desc_number.dst); 510 | SDL_RenderCopy(Renderer, alt_desc.Texture, NULL, &alt_desc.dst); 511 | SDL_RenderCopy(Renderer, alt_desc_number.Texture, NULL, &alt_desc_number.dst); 512 | SDL_RenderPresent(Renderer); 513 | //END RENDERING 514 | } 515 | 516 | void get_events(void) 517 | { 518 | //BEGIN EVENT LOOP 519 | while(SDL_PollEvent(&event)){ 520 | if(event.type == SDL_QUIT){ 521 | running =0; 522 | } 523 | if(event.type == SDL_MOUSEMOTION){ 524 | ; 525 | } 526 | if(event.type == SDL_MOUSEBUTTONDOWN){ 527 | if(event.button.button == SDL_BUTTON_RIGHT){ 528 | box.data.frac.pos.y = (float)wh-(real_meter_y*distance_m)-box.data.frac.size.y; 529 | box.data.dst.y = ceilf(box.data.frac.pos.y); 530 | 531 | } 532 | if(event.button.button == SDL_BUTTON_MIDDLE){ 533 | ; 534 | } 535 | if(event.button.button==SDL_BUTTON_LEFT){ 536 | pause=pause^1; //XOR - flip var 537 | break; 538 | } 539 | if(event.button.button==SDL_BUTTON_RIGHT){ 540 | time_acc=0; 541 | v=a=0; 542 | z=distance_m; 543 | update_desc(0, z); 544 | break; 545 | } 546 | } 547 | if(event.type == SDL_KEYDOWN ){ 548 | switch(event.key.keysym.sym ){ 549 | case SDLK_ESCAPE: 550 | running = 0; 551 | break; 552 | case SDLK_p: 553 | pause=pause^1; //XOR - flip var 554 | break; 555 | case SDLK_d: 556 | log_dt = log_dt^1; //XOR - flip var 557 | break; 558 | case SDLK_SPACE: 559 | break; 560 | default: 561 | break; 562 | } 563 | } 564 | } 565 | //END EVENT LOOP 566 | } 567 | //BEGIN HELPER 568 | void rect_round(struct entity_static *s_entity) 569 | { 570 | s_entity->dst.x=roundf(s_entity->frac.pos.x); 571 | s_entity->dst.y=roundf(s_entity->frac.pos.y); 572 | s_entity->dst.w=roundf(s_entity->frac.size.x); 573 | s_entity->dst.h=roundf(s_entity->frac.size.y); 574 | } 575 | //END HELPER 576 | 577 | //END FUNCTIONS 578 | -------------------------------------------------------------------------------- /src/Free Fall with velocity and accelaration.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from math import sqrt 4 | import matplotlib.pyplot as plt 5 | 6 | z_max = 50 # Starting height 7 | z = z_max # Current height 8 | g = 9.81 # m/s/s 9 | t = 0 # starting time 10 | t_max = sqrt(2 * z_max / g) 11 | print(t_max) 12 | v_max = g * t_max 13 | print(v_max) 14 | v = 0 # m/s, current velocity 15 | dt = 0.001 # time step 16 | a = 0 17 | H = [] 18 | T = [] 19 | V = [] 20 | A = [] 21 | while z > 0: 22 | z = z - v * dt - 0.5 * g * dt * dt 23 | print("Height", z) 24 | t = t + dt 25 | print("Time", t) 26 | v = v + g * dt 27 | print("Speed", v) 28 | a = v/t 29 | print("Acceleration", v) 30 | H.append(z) 31 | T.append(t) 32 | V.append(v) 33 | A.append(a) 34 | 35 | fig = plt.figure() 36 | plt.plot(T, H, label='Height at time') 37 | plt.plot(T, V, label='Velocity') 38 | plt.plot(T, A, label='Acceleration') 39 | plt.xlabel('Time in seconds') 40 | plt.ylabel('Height in metres') 41 | plt.title('Free Falling') 42 | plt.legend() 43 | fig.savefig("Free Fall v and a.png") 44 | plt.show() 45 | -------------------------------------------------------------------------------- /src/assets/Tower Crane.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/src/assets/Tower Crane.blend -------------------------------------------------------------------------------- /src/assets/Tower Crane.blend1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/src/assets/Tower Crane.blend1 -------------------------------------------------------------------------------- /src/helper.c: -------------------------------------------------------------------------------- 1 | #include "helper.h" 2 | SDL_Window *Window = NULL; 3 | SDL_Renderer *Renderer = NULL; 4 | 5 | void init(void) 6 | { 7 | 8 | SDL_Init(SDL_INIT_EVERYTHING); 9 | Window = SDL_CreateWindow("", 0, 0, 0, 0, SDL_WINDOW_HIDDEN); 10 | TTF_Init(); 11 | 12 | //BEGIN ICON 13 | SDL_Surface *icon; 14 | icon=IMG_Load("./assets/gfx/icon.png"); 15 | SDL_SetWindowIcon(Window, icon); 16 | SDL_FreeSurface(icon); 17 | //END ICON 18 | 19 | Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); 20 | SDL_SetRenderDrawBlendMode(Renderer, SDL_BLENDMODE_BLEND); 21 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); 22 | 23 | } 24 | 25 | void exit_(void) 26 | { 27 | TTF_Quit(); 28 | SDL_DestroyRenderer(Renderer); 29 | SDL_DestroyWindow(Window); 30 | SDL_Quit(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/helper.h: -------------------------------------------------------------------------------- 1 | #ifndef _HELPER_H_ 2 | #define _HELPER_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | SDL_Window *Window; 10 | SDL_Renderer *Renderer; 11 | 12 | void init (void); 13 | void exit_(void); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* This is a light SDL2 template 5 | * 6 | */ 7 | 8 | /* DEFINED PROGRESS GOALS 9 | * 10 | * None atm 11 | * 12 | */ 13 | //END DESCRIPTION 14 | 15 | //BEGIN INCLUDES 16 | //system headers 17 | #include 18 | //local headers 19 | #include "helper.h" 20 | //END INCLUDES 21 | 22 | //BEGIN CPP DEFINITIONS 23 | #define WHITE 255,255,255,255 24 | #define BLACK 0,0,0,255 25 | #define BLUE 0,0,255,255 26 | #define RED 255,0,0,255 27 | #define WW 1000 // dezimeter = 100m 28 | #define WH 1000 // dezimeter = 100m 29 | //END CPP DEFINITIONS 30 | 31 | //BEGIN DATASTRUCTURES 32 | //END DATASTRUCTURES 33 | 34 | //BEGIN GLOBALS 35 | int ww=WW; 36 | int wh=WH; 37 | float boxLocation; // horizontal location of box 38 | float boxVelocity; // horizontal velocity of box 39 | float boxWidth; // width of box in pixels. 40 | float initialAltitude; 41 | float ballAltitude; // vertical location of ball 42 | float ballLocation; // horizontal location of ball 43 | float ball_mass = 0.1; 44 | float g; // gravitational acceleration 45 | float time; // elapsed time. 46 | float timeIncrement; 47 | 48 | SDL_Rect box; 49 | SDL_Rect ball; 50 | 51 | //BEGIN VISIBLES 52 | SDL_Surface *temp_surface = NULL; 53 | 54 | SDL_Texture *logo = NULL; 55 | SDL_Texture *gepard = NULL; 56 | SDL_Rect logo_dst; 57 | //END VISIBLES 58 | 59 | SDL_Point mouse; 60 | 61 | //END GLOBALS 62 | 63 | //BEGIN FUNCTION PROTOTYPES 64 | float time_(void); 65 | void assets_in (void); 66 | void assets_out (void); 67 | //END FUNCTION PROTOTYPES 68 | 69 | //END HEAD 70 | 71 | //BEGIN MAIN FUNCTION 72 | int main(int argc, char *argv[]) 73 | { 74 | 75 | (void)argc; 76 | (void)argv; 77 | 78 | //BEGIN INIT 79 | init(); 80 | assets_in(); 81 | 82 | //BEGIN WINDOW 83 | SDL_SetWindowPosition(Window,0,0); 84 | SDL_SetWindowSize(Window,ww,wh); 85 | SDL_SetWindowTitle(Window, "SDL2 Template"); 86 | SDL_ShowWindow(Window); 87 | //END WINDOW 88 | 89 | SDL_Event event; 90 | int running = 1; 91 | float currentTime = 0.0f; 92 | float accumulator = 0.0f; 93 | 94 | //END INIT 95 | 96 | //BEGIN MAIN LOOP 97 | while(running){ 98 | 99 | //BEGIN EVENT LOOP 100 | while(SDL_PollEvent(&event)){ 101 | if(event.type == SDL_QUIT){ 102 | running =0; 103 | } 104 | if(event.type == SDL_MOUSEMOTION){ 105 | ; 106 | } 107 | if(event.type == SDL_MOUSEBUTTONDOWN){ 108 | if(event.button.button == SDL_BUTTON_RIGHT){ 109 | ; 110 | } 111 | if(event.button.button == SDL_BUTTON_MIDDLE){ 112 | ; 113 | } 114 | if(event.button.button==SDL_BUTTON_LEFT){ 115 | ; 116 | } 117 | } 118 | if(event.type == SDL_KEYDOWN ){ 119 | switch(event.key.keysym.sym ){ 120 | case SDLK_ESCAPE: 121 | running =0; 122 | break; 123 | 124 | case SDLK_r: 125 | case SDLK_BACKSPACE: 126 | break; 127 | 128 | case SDLK_p: 129 | case SDLK_SPACE: 130 | break; 131 | 132 | default: 133 | break; 134 | } 135 | } 136 | } 137 | //END EVENT LOOP 138 | 139 | //BEGIN LOGIC 140 | float newTime = time_(); 141 | float deltaTime = newTime - currentTime; 142 | currentTime = newTime; 143 | // accumulator += deltaTime; 144 | 145 | if (ballAltitude > 0) { 146 | time += deltaTime; 147 | 148 | boxLocation = boxVelocity*time; 149 | box.x=roundf(boxLocation); 150 | 151 | ballAltitude = initialAltitude - 5 * g * time * time; 152 | ball.y= WH-roundf(ballAltitude); 153 | SDL_Log("ball.y: %d", ball.y); 154 | SDL_Log("time=%f boxLocation=%f ballAltitude=%f\n", 155 | time, boxLocation, ballAltitude); 156 | // SDL_Log("accumulator: %f", accumulator); 157 | } /*else 158 | running = 0;*/ 159 | //END LOGIC 160 | 161 | //BEGIN RENDERING 162 | SDL_SetRenderDrawColor(Renderer, WHITE); 163 | SDL_RenderClear(Renderer); 164 | // SDL_SetRenderDrawColor(Renderer, BLUE); 165 | // SDL_RenderFillRect(Renderer, &box); 166 | SDL_SetRenderDrawColor(Renderer, RED); 167 | SDL_RenderFillRect(Renderer, &ball); 168 | SDL_RenderCopy(Renderer, logo, NULL, &logo_dst); 169 | SDL_RenderCopy(Renderer, gepard, NULL, &box); 170 | SDL_RenderPresent(Renderer); 171 | //END RENDERING 172 | 173 | } 174 | //END MAIN LOOP 175 | 176 | assets_out(); 177 | exit_(); 178 | return EXIT_SUCCESS; 179 | 180 | } 181 | //END MAIN FUNCTION 182 | 183 | //BEGIN FUNCTIONS 184 | float time_(void) 185 | { 186 | 187 | static Uint64 start = 0; 188 | static float frequency = 0; 189 | 190 | if (start==0){ 191 | start = SDL_GetPerformanceCounter(); 192 | frequency= (float)SDL_GetPerformanceFrequency(); 193 | return 0.0f; 194 | } 195 | 196 | Uint64 counter = 0; 197 | counter=SDL_GetPerformanceCounter(); 198 | return (((float)counter - (float)start) /frequency); 199 | // real64 MSPerFrame = (((1000.0f * (real64)CounterElapsed) / (real64)PerCountFrequency)); 200 | } 201 | 202 | void assets_in(void) 203 | { 204 | 205 | int w,h; 206 | //BEGIN LOGO 207 | temp_surface = IMG_Load("./assets/gfx/logo.png"); 208 | logo = SDL_CreateTextureFromSurface(Renderer, temp_surface); 209 | SDL_QueryTexture(logo, NULL, NULL, &logo_dst.w, &logo_dst.h); 210 | logo_dst.x=(ww/2)-(logo_dst.w/2); 211 | logo_dst.y=(wh/2)-(logo_dst.h/2); 212 | //END LOGO 213 | 214 | 215 | //BEGIN LOGO 216 | temp_surface = IMG_Load("./assets/gfx/gepard.png"); 217 | gepard = SDL_CreateTextureFromSurface(Renderer, temp_surface); 218 | SDL_QueryTexture(gepard, NULL, NULL, &w, &h); 219 | SDL_Log("w: %d, h: %d",w,h); 220 | //END LOGO 221 | // Set initial values. 222 | boxLocation = 0.0; 223 | box.x = roundf(boxLocation); 224 | 225 | boxVelocity = 200; //dezimeter pro sekunde oder 20m p/s o. 72 km/h 226 | boxWidth = 15; 227 | box.w = roundf(boxWidth); 228 | float he = (float)h/((float)w/15); 229 | box.h = roundf(he); 230 | box.y = WH-box.h; 231 | 232 | initialAltitude = WH; 233 | ballAltitude = initialAltitude; 234 | ball.w=4; 235 | ball.h=4; 236 | ball.y=0+4; 237 | ballLocation = 800; 238 | ball.x=ballLocation; 239 | time = 0.0; 240 | // m/s*s 241 | g = 9.81; // Earth 242 | // g = 1.624; // Moon 243 | // g = 24.8; // Jupiter 244 | } 245 | 246 | void assets_out(void) 247 | { 248 | SDL_DestroyTexture(logo); 249 | SDL_DestroyTexture(gepard); 250 | } 251 | 252 | //END FUNCTIONS 253 | -------------------------------------------------------------------------------- /src/x1.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* DESCRIPTION: 5 | * 6 | * Gravity 7 | * 8 | * DEFINED PROGRESS GOALS: 9 | * 10 | * None atm 11 | * 12 | */ 13 | //END DESCRIPTION 14 | 15 | //BEGIN INCLUDES 16 | //system headers 17 | #include 18 | //local headers 19 | #include "helper.h" 20 | //END INCLUDES 21 | 22 | //BEGIN CPP DEFINITIONS 23 | //Some Color Helpers 24 | //R G B 25 | #define RED 255,0,0,255 26 | #define GREEN 0,255,0,255 27 | #define BLUE 0,0,255,255 28 | //All on 29 | #define WHITE 255,255,255,255 30 | //All off 31 | #define BLACK 0,0,0,255 32 | 33 | 34 | //Real Size in SI-Unit centimeters 35 | #define REALSIZE_H 13.0 36 | #define REALSIZE_V 9.0 37 | 38 | //DPI is Dots per Inch - Industry should fix that 39 | #define CENTI_PER_INCH 2.54 40 | //END CPP DEFINITIONS 41 | 42 | //BEGIN DATASTRUCTURES 43 | struct scalar2{ 44 | float x; 45 | float y; 46 | }; 47 | //END DATASTRUCTURES 48 | 49 | //BEGIN GLOBALS 50 | int ww; 51 | int wh; 52 | 53 | struct scalar2 dpi; 54 | SDL_Rect box; 55 | 56 | //BEGIN VISIBLES 57 | //END VISIBLES 58 | 59 | //END GLOBALS 60 | 61 | //BEGIN FUNCTION PROTOTYPES 62 | void query_disp (void); 63 | void assets_in (void); 64 | //END FUNCTION PROTOTYPES 65 | 66 | //END HEAD 67 | 68 | //BEGIN MAIN FUNCTION 69 | int main(int argc, char *argv[]) 70 | { 71 | 72 | (void)argc; 73 | (void)argv; 74 | 75 | //BEGIN INIT 76 | init(); 77 | //BEGIN WINDOW 78 | // SDL_SetWindowPosition(Window,0,0); 79 | // SDL_SetWindowSize(Window,ww,wh); 80 | char title[10]; 81 | query_disp(); 82 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 83 | sprintf(title, "%dx%d cm",(int)REALSIZE_H,(int)REALSIZE_V); 84 | SDL_SetWindowTitle(Window, title); 85 | SDL_ShowWindow(Window); 86 | //END WINDOW 87 | 88 | //giving Box a size in centimeters 89 | box.w = roundf(2.0/CENTI_PER_INCH*dpi.x); 90 | box.h = roundf(2.0/CENTI_PER_INCH*dpi.y); 91 | 92 | box.x = ww/2-box.w/2; 93 | box.y = wh/2-box.h/2; 94 | 95 | SDL_Event event; 96 | int running = 1; 97 | //END INIT 98 | 99 | //BEGIN MAIN LOOP 100 | while(running){ 101 | 102 | //BEGIN EVENT LOOP 103 | while(SDL_PollEvent(&event)){ 104 | if(event.type == SDL_QUIT){ 105 | running =0; 106 | } 107 | if(event.type == SDL_MOUSEMOTION){ 108 | ; 109 | } 110 | if(event.type == SDL_MOUSEBUTTONDOWN){ 111 | if(event.button.button == SDL_BUTTON_RIGHT){ 112 | ; 113 | } 114 | if(event.button.button == SDL_BUTTON_MIDDLE){ 115 | ; 116 | } 117 | if(event.button.button==SDL_BUTTON_LEFT){ 118 | ; 119 | } 120 | } 121 | if(event.type == SDL_KEYDOWN ){ 122 | switch(event.key.keysym.sym ){ 123 | case SDLK_ESCAPE: 124 | running =0; 125 | break; 126 | 127 | case SDLK_r: 128 | case SDLK_BACKSPACE: 129 | break; 130 | 131 | case SDLK_p: 132 | case SDLK_SPACE: 133 | break; 134 | 135 | default: 136 | break; 137 | } 138 | } 139 | } 140 | //END EVENT LOOP 141 | 142 | //BEGIN LOGIC 143 | //END LOGIC 144 | 145 | //BEGIN RENDERING 146 | SDL_SetRenderDrawColor(Renderer, WHITE); 147 | SDL_RenderClear(Renderer); 148 | 149 | SDL_SetRenderDrawColor(Renderer, BLUE); 150 | SDL_RenderFillRect(Renderer, &box); 151 | 152 | SDL_RenderPresent(Renderer); 153 | //END RENDERING 154 | 155 | } 156 | //END MAIN LOOP 157 | exit_(); 158 | return EXIT_SUCCESS; 159 | 160 | } 161 | //END MAIN FUNCTION 162 | 163 | //BEGIN FUNCTIONS 164 | void query_disp (void) 165 | { 166 | int disp; 167 | 168 | disp=SDL_GetWindowDisplayIndex(Window); 169 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 170 | 171 | SDL_Log("hdpi: %f", dpi.x); 172 | ww=roundf(REALSIZE_H/CENTI_PER_INCH*dpi.x); 173 | 174 | SDL_Log("vdpi: %f", dpi.y); 175 | wh=roundf(REALSIZE_V/CENTI_PER_INCH*dpi.y); 176 | SDL_Log("w: %d, h: %d",ww,wh); 177 | SDL_SetWindowSize(Window, ww,wh); 178 | 179 | } 180 | //END FUNCTIONS 181 | -------------------------------------------------------------------------------- /src/x2.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* Window Size in SI-Units 5 | * 6 | */ 7 | 8 | /* DEFINED PROGRESS GOALS 9 | * 10 | * None atm 11 | * 12 | */ 13 | //END DESCRIPTION 14 | 15 | //BEGIN INCLUDES 16 | //system headers 17 | #include 18 | //local headers 19 | #include "helper.h" 20 | //END INCLUDES 21 | 22 | //BEGIN CPP DEFINITIONS 23 | //Some Color Helpers 24 | //R G B 25 | #define RED 255,0,0,255 26 | #define GREEN 0,255,0,255 27 | #define BLUE 0,0,255,255 28 | //All on 29 | #define WHITE 255,255,255,255 30 | //All off 31 | #define BLACK 0,0,0,255 32 | 33 | 34 | //Real Size in SI-Unit centimeters 35 | #define REALSIZE_H 13.0 36 | #define REALSIZE_V 9.0 37 | 38 | //temporarily Window Size 39 | #define WW 100 40 | #define WH 100 41 | 42 | 43 | #define CENTI_PER_INCH 2.54 44 | //10 pixel == 1 m 45 | //0,277778 Meter pro Sekunde == 1km/h 46 | // 47 | //END CPP DEFINITIONS 48 | 49 | //BEGIN DATASTRUCTURES 50 | struct scalar2{ 51 | float x; 52 | float y; 53 | }; 54 | //END DATASTRUCTURES 55 | 56 | //BEGIN GLOBALS 57 | int ww=WW; 58 | int wh=WH; 59 | 60 | struct scalar2 dpi; 61 | float boxLocation; // horizontal location of box 62 | float boxVelocity; // horizontal velocity of box 63 | float boxWidth; // width of box in pixels. 64 | 65 | float time; // elapsed time. 66 | float timeIncrement; 67 | 68 | SDL_Rect box; 69 | SDL_Rect box2; 70 | //BEGIN VISIBLES 71 | SDL_Surface *temp_surface = NULL; 72 | SDL_Texture *gepard = NULL; 73 | //END VISIBLES 74 | 75 | //END GLOBALS 76 | 77 | //BEGIN FUNCTION PROTOTYPES 78 | float time_(void); 79 | void query_disp (void); 80 | void assets_in (void); 81 | void assets_out (void); 82 | //END FUNCTION PROTOTYPES 83 | 84 | //END HEAD 85 | 86 | //BEGIN MAIN FUNCTION 87 | int main(int argc, char *argv[]) 88 | { 89 | 90 | (void)argc; 91 | (void)argv; 92 | 93 | //BEGIN INIT 94 | init(); 95 | //BEGIN WINDOW 96 | SDL_SetWindowPosition(Window,0,0); 97 | SDL_SetWindowSize(Window,ww,wh); 98 | SDL_SetWindowTitle(Window, "Window in centimetres"); 99 | SDL_ShowWindow(Window); 100 | //END WINDOW 101 | query_disp(); 102 | SDL_SetWindowPosition(Window,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED); 103 | assets_in(); 104 | SDL_Event event; 105 | int running = 1; 106 | float currentTime = 0.0f; 107 | float accumulator = 0.0f; 108 | //END INIT 109 | 110 | //BEGIN MAIN LOOP 111 | while(running){ 112 | 113 | //BEGIN EVENT LOOP 114 | while(SDL_PollEvent(&event)){ 115 | if(event.type == SDL_QUIT){ 116 | running =0; 117 | } 118 | if(event.type == SDL_MOUSEMOTION){ 119 | ; 120 | } 121 | if(event.type == SDL_MOUSEBUTTONDOWN){ 122 | if(event.button.button == SDL_BUTTON_RIGHT){ 123 | ; 124 | } 125 | if(event.button.button == SDL_BUTTON_MIDDLE){ 126 | ; 127 | } 128 | if(event.button.button==SDL_BUTTON_LEFT){ 129 | boxLocation=0; 130 | box.x=roundf(boxLocation); 131 | } 132 | } 133 | if(event.type == SDL_KEYDOWN ){ 134 | switch(event.key.keysym.sym ){ 135 | case SDLK_ESCAPE: 136 | running =0; 137 | break; 138 | 139 | case SDLK_r: 140 | case SDLK_BACKSPACE: 141 | break; 142 | 143 | case SDLK_p: 144 | case SDLK_SPACE: 145 | break; 146 | 147 | default: 148 | break; 149 | } 150 | } 151 | } 152 | //END EVENT LOOP 153 | 154 | //BEGIN LOGIC 155 | float newTime = time_(); 156 | float deltaTime = newTime - currentTime; 157 | currentTime = newTime; 158 | accumulator += deltaTime; 159 | if (box.x < ww) { 160 | boxLocation+= boxVelocity*deltaTime; 161 | box.x=roundf(boxLocation); 162 | } 163 | //END LOGIC 164 | 165 | //BEGIN RENDERING 166 | SDL_SetRenderDrawColor(Renderer, WHITE); 167 | SDL_RenderClear(Renderer); 168 | SDL_SetRenderDrawColor(Renderer, BLUE); 169 | SDL_RenderFillRect(Renderer, &box); 170 | SDL_SetRenderDrawColor(Renderer, RED); 171 | SDL_RenderFillRect(Renderer, &box2); 172 | SDL_RenderCopy(Renderer, gepard, NULL, &box); 173 | SDL_RenderPresent(Renderer); 174 | //END RENDERING 175 | 176 | } 177 | //END MAIN LOOP 178 | 179 | assets_out(); 180 | exit_(); 181 | return EXIT_SUCCESS; 182 | 183 | } 184 | //END MAIN FUNCTION 185 | 186 | //BEGIN FUNCTIONS 187 | float time_(void) 188 | { 189 | 190 | static Uint64 start = 0; 191 | static float frequency = 0; 192 | 193 | if (start==0){ 194 | start = SDL_GetPerformanceCounter(); 195 | frequency= (float)SDL_GetPerformanceFrequency(); 196 | return 0.0f; 197 | } 198 | 199 | Uint64 counter = 0; 200 | counter=SDL_GetPerformanceCounter(); 201 | return (((float)counter - (float)start) /frequency); 202 | // real64 MSPerFrame = (((1000.0f * (real64)CounterElapsed) / (real64)PerCountFrequency)); 203 | } 204 | 205 | void assets_in(void) 206 | { 207 | 208 | int w,h; 209 | 210 | temp_surface = IMG_Load("./assets/gfx/gepard.png"); 211 | gepard = SDL_CreateTextureFromSurface(Renderer, temp_surface); 212 | SDL_QueryTexture(gepard, NULL, NULL, &w, &h); 213 | 214 | boxLocation = 0.0; 215 | box.x = roundf(boxLocation); 216 | 217 | boxVelocity = 200; //dezimeter pro sekunde oder 20m p/s o. 72 km/h 218 | boxWidth = 15; 219 | box.w = roundf(boxWidth); 220 | float he = (float)h/((float)w/15); 221 | box.h = roundf(he); 222 | box.y = wh-box.h; 223 | 224 | //giving Box size in Centimeters 225 | box2.w = roundf(2.0/CENTI_PER_INCH*dpi.x); 226 | box2.h = roundf(2.0/CENTI_PER_INCH*dpi.y); 227 | 228 | box2.x = ww/2-box2.w/2; 229 | box2.y = wh/2-box2.h/2; 230 | } 231 | 232 | void assets_out(void) 233 | { 234 | SDL_DestroyTexture(gepard); 235 | } 236 | void query_disp (void) 237 | { 238 | int disp; 239 | 240 | disp=SDL_GetWindowDisplayIndex(Window); 241 | SDL_GetDisplayDPI(disp, NULL,&dpi.x, &dpi.y); 242 | 243 | SDL_Log("hdpi: %f", dpi.x); 244 | ww=roundf(SCALE_SIZE_H/CENTI_PER_INCH*dpi.x); 245 | 246 | SDL_Log("vdpi: %f", dpi.y); 247 | wh=roundf(REALSIZE_V/CENTI_PER_INCH*dpi.y); 248 | SDL_Log("w: %d, h: %d",ww,wh); 249 | SDL_SetWindowSize(Window, ww,wh); 250 | 251 | } 252 | //END FUNCTIONS 253 | -------------------------------------------------------------------------------- /src/x3.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* Acceleration 5 | * 6 | */ 7 | 8 | /* DEFINED PROGRESS GOALS 9 | * 10 | * None atm 11 | * 12 | */ 13 | //END DESCRIPTION 14 | 15 | //BEGIN INCLUDES 16 | //system headers 17 | #include 18 | //local headers 19 | #include "helper.h" 20 | //END INCLUDES 21 | 22 | //BEGIN CPP DEFINITIONS 23 | #define WHITE 255,255,255,255 24 | #define BLACK 0,0,0,255 25 | #define BLUE 0,0,255,255 26 | #define RED 255,0,0,255 27 | #define WW 1000 // dezimeter = 100m 28 | #define WH 100 // dezimeter = 10m 29 | //END CPP DEFINITIONS 30 | 31 | //BEGIN DATASTRUCTURES 32 | //END DATASTRUCTURES 33 | 34 | //BEGIN GLOBALS 35 | int ww=WW; 36 | int wh=WH; 37 | float boxLocation; // horizontal location of box 38 | float boxVelocity; // horizontal velocity of box 39 | float acceleration; 40 | float boxWidth; // width of box in pixels. 41 | 42 | float time; // elapsed time. 43 | float timeIncrement; 44 | 45 | SDL_Rect box; 46 | 47 | //BEGIN VISIBLES 48 | SDL_Surface *temp_surface = NULL; 49 | SDL_Texture *gepard = NULL; 50 | //END VISIBLES 51 | 52 | //END GLOBALS 53 | 54 | //BEGIN FUNCTION PROTOTYPES 55 | float time_(void); 56 | void assets_in (void); 57 | void assets_out (void); 58 | //END FUNCTION PROTOTYPES 59 | 60 | //END HEAD 61 | 62 | //BEGIN MAIN FUNCTION 63 | int main(int argc, char *argv[]) 64 | { 65 | 66 | (void)argc; 67 | (void)argv; 68 | 69 | //BEGIN INIT 70 | init(); 71 | assets_in(); 72 | 73 | //BEGIN WINDOW 74 | SDL_SetWindowPosition(Window,0,0); 75 | SDL_SetWindowSize(Window,ww,wh); 76 | SDL_SetWindowTitle(Window, "SDL2 Template"); 77 | SDL_ShowWindow(Window); 78 | //END WINDOW 79 | 80 | SDL_Event event; 81 | int running = 1; 82 | float currentTime = 0.0f; 83 | float accumulator = 0.0f; 84 | 85 | //END INIT 86 | 87 | //BEGIN MAIN LOOP 88 | while(running){ 89 | 90 | //BEGIN EVENT LOOP 91 | while(SDL_PollEvent(&event)){ 92 | if(event.type == SDL_QUIT){ 93 | running =0; 94 | } 95 | if(event.type == SDL_MOUSEMOTION){ 96 | ; 97 | } 98 | if(event.type == SDL_MOUSEBUTTONDOWN){ 99 | if(event.button.button == SDL_BUTTON_RIGHT){ 100 | ; 101 | } 102 | if(event.button.button == SDL_BUTTON_MIDDLE){ 103 | ; 104 | } 105 | if(event.button.button==SDL_BUTTON_LEFT){ 106 | boxLocation=0; 107 | box.x=roundf(boxLocation); 108 | } 109 | } 110 | if(event.type == SDL_KEYDOWN ){ 111 | switch(event.key.keysym.sym ){ 112 | case SDLK_ESCAPE: 113 | running =0; 114 | break; 115 | 116 | case SDLK_r: 117 | case SDLK_BACKSPACE: 118 | break; 119 | 120 | case SDLK_p: 121 | case SDLK_SPACE: 122 | break; 123 | 124 | default: 125 | break; 126 | } 127 | } 128 | } 129 | //END EVENT LOOP 130 | 131 | //BEGIN LOGIC 132 | float newTime = time_(); 133 | float deltaTime = newTime - currentTime; 134 | currentTime = newTime; 135 | accumulator += deltaTime; 136 | if (box.x < ww) { 137 | boxVelocity+= 138 | boxLocation+= boxVelocity*deltaTime; 139 | box.x=roundf(boxLocation); 140 | } 141 | //END LOGIC 142 | 143 | //BEGIN RENDERING 144 | SDL_SetRenderDrawColor(Renderer, WHITE); 145 | SDL_RenderClear(Renderer); 146 | SDL_SetRenderDrawColor(Renderer, BLUE); 147 | SDL_RenderFillRect(Renderer, &box); 148 | SDL_RenderCopy(Renderer, gepard, NULL, &box); 149 | SDL_RenderPresent(Renderer); 150 | //END RENDERING 151 | 152 | } 153 | //END MAIN LOOP 154 | 155 | assets_out(); 156 | exit_(); 157 | return EXIT_SUCCESS; 158 | 159 | } 160 | //END MAIN FUNCTION 161 | 162 | //BEGIN FUNCTIONS 163 | float time_(void) 164 | { 165 | 166 | static Uint64 start = 0; 167 | static float frequency = 0; 168 | 169 | if (start==0){ 170 | start = SDL_GetPerformanceCounter(); 171 | frequency= (float)SDL_GetPerformanceFrequency(); 172 | return 0.0f; 173 | } 174 | 175 | Uint64 counter = 0; 176 | counter=SDL_GetPerformanceCounter(); 177 | return (((float)counter - (float)start) /frequency); 178 | // real64 MSPerFrame = (((1000.0f * (real64)CounterElapsed) / (real64)PerCountFrequency)); 179 | } 180 | 181 | void assets_in(void) 182 | { 183 | 184 | int w,h; 185 | 186 | temp_surface = IMG_Load("./assets/gfx/gepard.png"); 187 | gepard = SDL_CreateTextureFromSurface(Renderer, temp_surface); 188 | SDL_QueryTexture(gepard, NULL, NULL, &w, &h); 189 | SDL_Log("w: %d, h: %d",w,h); 190 | 191 | boxLocation = 0.0; 192 | box.x = roundf(boxLocation); 193 | 194 | boxVelocity = 0; //dezimeter pro sekunde oder 20m p/s o. 72 km/h 195 | boxWidth = 15; 196 | box.w = roundf(boxWidth); 197 | float he = (float)h/((float)w/15); 198 | box.h = roundf(he); 199 | box.y = WH-box.h; 200 | } 201 | 202 | void assets_out(void) 203 | { 204 | SDL_DestroyTexture(gepard); 205 | } 206 | 207 | //END FUNCTIONS 208 | -------------------------------------------------------------------------------- /src/x4.c: -------------------------------------------------------------------------------- 1 | //BEGIN HEAD 2 | //BEGIN DESCRIPTION 3 | 4 | /* This is a light SDL2 template 5 | * 6 | */ 7 | 8 | /* DEFINED PROGRESS GOALS 9 | * 10 | * None atm 11 | * 12 | */ 13 | //END DESCRIPTION 14 | 15 | //BEGIN INCLUDES 16 | //system headers 17 | #include 18 | //local headers 19 | #include "helper.h" 20 | //END INCLUDES 21 | 22 | //BEGIN CPP DEFINITIONS 23 | #define WHITE 255,255,255,255 24 | #define BLACK 0,0,0,255 25 | #define BLUE 0,0,255,255 26 | #define RED 255,0,0,255 27 | #define WW 550 28 | #define WH (WW/16)*12 29 | //END CPP DEFINITIONS 30 | 31 | //BEGIN DATASTRUCTURES 32 | //END DATASTRUCTURES 33 | 34 | //BEGIN GLOBALS 35 | int ww=WW; 36 | int wh=WH; 37 | float boxLocation; // horizontal location of box 38 | float boxVelocity; // horizontal velocity of box 39 | int boxWidth; // width of box in pixels. 40 | float initialAltitude; 41 | float ballAltitude; // vertical location of ball 42 | float ballLocation; // horizontal location of ball 43 | float ball_mass = 0.1; 44 | float g; // gravitational acceleration 45 | float time; // elapsed time. 46 | float timeIncrement; 47 | 48 | SDL_Rect box; 49 | SDL_Rect ball; 50 | 51 | //BEGIN VISIBLES 52 | SDL_Surface *temp_surface = NULL; 53 | 54 | SDL_Texture *logo = NULL; 55 | SDL_Rect logo_dst; 56 | //END VISIBLES 57 | 58 | SDL_Point mouse; 59 | 60 | //END GLOBALS 61 | 62 | //BEGIN FUNCTION PROTOTYPES 63 | float time_(void); 64 | void assets_in (void); 65 | void assets_out (void); 66 | //END FUNCTION PROTOTYPES 67 | 68 | //END HEAD 69 | 70 | //BEGIN MAIN FUNCTION 71 | int main(int argc, char *argv[]) 72 | { 73 | 74 | (void)argc; 75 | (void)argv; 76 | 77 | //BEGIN INIT 78 | init(); 79 | assets_in(); 80 | 81 | //BEGIN WINDOW 82 | SDL_SetWindowPosition(Window,0,0); 83 | SDL_SetWindowSize(Window,ww,wh); 84 | SDL_SetWindowTitle(Window, "SDL2 Template"); 85 | SDL_ShowWindow(Window); 86 | //END WINDOW 87 | 88 | SDL_Event event; 89 | int running = 1; 90 | //END INIT 91 | 92 | //BEGIN MAIN LOOP 93 | while(running){ 94 | 95 | //BEGIN EVENT LOOP 96 | while(SDL_PollEvent(&event)){ 97 | if(event.type == SDL_QUIT){ 98 | running =0; 99 | } 100 | if(event.type == SDL_MOUSEMOTION){ 101 | ; 102 | } 103 | if(event.type == SDL_MOUSEBUTTONDOWN){ 104 | if(event.button.button == SDL_BUTTON_RIGHT){ 105 | ; 106 | } 107 | if(event.button.button == SDL_BUTTON_MIDDLE){ 108 | ; 109 | } 110 | if(event.button.button==SDL_BUTTON_LEFT){ 111 | ; 112 | } 113 | } 114 | if(event.type == SDL_KEYDOWN ){ 115 | switch(event.key.keysym.sym ){ 116 | case SDLK_ESCAPE: 117 | running =0; 118 | break; 119 | 120 | case SDLK_r: 121 | case SDLK_BACKSPACE: 122 | break; 123 | 124 | case SDLK_p: 125 | case SDLK_SPACE: 126 | break; 127 | 128 | default: 129 | break; 130 | } 131 | } 132 | } 133 | //END EVENT LOOP 134 | 135 | //BEGIN LOGIC 136 | if (ballAltitude < WH) { 137 | time += time_(); 138 | boxLocation = boxVelocity*time; 139 | box.x=roundf(boxLocation); 140 | 141 | ballAltitude = initialAltitude + ball_mass * g * time * time; 142 | ball.y= roundf(ballAltitude); 143 | SDL_Log("ball.y: %d", ball.y); 144 | SDL_Log("time=%lf boxLocation=%lf ballAltitude=%lf\n", 145 | time, boxLocation, ballAltitude); 146 | } 147 | //END LOGIC 148 | 149 | //BEGIN RENDERING 150 | SDL_SetRenderDrawColor(Renderer, WHITE); 151 | SDL_RenderClear(Renderer); 152 | SDL_SetRenderDrawColor(Renderer, BLUE); 153 | SDL_RenderFillRect(Renderer, &box); 154 | SDL_SetRenderDrawColor(Renderer, RED); 155 | SDL_RenderFillRect(Renderer, &ball); 156 | SDL_RenderCopy(Renderer, logo, NULL, &logo_dst); 157 | 158 | SDL_RenderPresent(Renderer); 159 | //END RENDERING 160 | } 161 | //END MAIN LOOP 162 | 163 | assets_out(); 164 | exit_(); 165 | return EXIT_SUCCESS; 166 | 167 | } 168 | //END MAIN FUNCTION 169 | 170 | //BEGIN FUNCTIONS 171 | float time_(void) 172 | { 173 | 174 | static Uint64 start = 0; 175 | static Uint64 frequency = 0; 176 | 177 | if (start==0){ 178 | start=SDL_GetPerformanceCounter(); 179 | frequency=SDL_GetPerformanceFrequency(); 180 | return 0.0f; 181 | } 182 | 183 | Uint64 counter = 0; 184 | counter=SDL_GetPerformanceCounter(); 185 | return (float) ((counter - start) / (double)frequency); 186 | } 187 | 188 | void assets_in(void) 189 | { 190 | 191 | //BEGIN LOGO 192 | temp_surface = IMG_Load("./assets/gfx/logo.png"); 193 | logo = SDL_CreateTextureFromSurface(Renderer, temp_surface); 194 | SDL_QueryTexture(logo, NULL, NULL, &logo_dst.w, &logo_dst.h); 195 | logo_dst.x=(ww/2)-(logo_dst.w/2); 196 | logo_dst.y=(wh/2)-(logo_dst.h/2); 197 | //END LOGO 198 | 199 | // Set initial values. 200 | boxLocation = 0.0; 201 | box.x=roundf(boxLocation); 202 | 203 | boxVelocity = 12; 204 | boxWidth = 40; 205 | box.w=roundf(boxWidth); 206 | box.h=box.w/4; 207 | box.y=WH-box.h; 208 | 209 | initialAltitude = 0; 210 | ballAltitude = initialAltitude; 211 | ball.w=4; 212 | ball.h=4; 213 | ball.y=0+4; 214 | ballLocation = 210.0; 215 | ball.x=ballLocation; 216 | time = 0.0; 217 | // g = 9.81; // Earth 218 | g = 1.624; // Moon 219 | // g = 24.8; // Jupiter 220 | } 221 | 222 | void assets_out(void) 223 | { 224 | SDL_DestroyTexture(logo); 225 | } 226 | 227 | //END FUNCTIONS 228 | -------------------------------------------------------------------------------- /test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/test.jpg -------------------------------------------------------------------------------- /time1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Acry/SDL2-Physics/b41534165466abd014f4541a2f452b0bedc7a552/time1.png --------------------------------------------------------------------------------