123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- /*
- * 2D Physic Engine
- * app.cpp: basic application handling.
- * Based on pikuma.com Learn Game Physics Engine Programming course.
- * Copyright (c) 2022 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 07/06/2022.
- */
- #include <app.h>
- #include <graphics.h>
- #include <imgui.h>
- #include <imgui_sdl.h>
- #include <imgui_impl_sdl.h>
- #include <physics/constants.h>
- void application::parseParameters(int argc, char *argv[])
- {
- // Nothing to do for now
- }
- void application::setup()
- {
- this->running = graphics::openWindow();
- this->millisecsPreviousFrame = SDL_GetTicks();
- this->particles.push_back(new particle(50, 100, 1.0, 4));
- this->particles.push_back(new particle(50, 200, 3.0, 12));
- }
- void application::input()
- {
- SDL_Event event;
- while(SDL_PollEvent(&event))
- {
- //ImGui_ImplSDL2_ProcessEvent(&event);
- //ImGuiIO &io = ImGui::GetIO();
- switch(event.type)
- {
- case SDL_QUIT:
- this->running = false;
- break;
- case SDL_KEYDOWN:
- if (event.key.keysym.sym == SDLK_ESCAPE)
- {
- this->running = false;
- }
- break;
- }
- }
- }
- double application::syncAndDeltatime()
- {
- /* Let's make sure we are running at the expected framerate */
- uint32_t now = SDL_GetTicks();
- int32_t timeToWait = MILLISECS_PER_FRAME - (now - this->millisecsPreviousFrame);
- if ((timeToWait > 0) && (timeToWait <= MILLISECS_PER_FRAME))
- {
- SDL_Delay(timeToWait);
- }
- double deltaTime = (SDL_GetTicks() - this->millisecsPreviousFrame) / 1000.0;
- /* clamp maximum deltatime to avoid weird behaviours */
- if (deltaTime > 0.016)
- {
- deltaTime = 0.016;
- }
- this->millisecsPreviousFrame = SDL_GetTicks();
- return deltaTime;
- }
- void application::update()
- {
- vec2 wind = vec2(0.2 * PIXELS_PER_METER, 0);
- double deltaTime = this->syncAndDeltatime();
- this->part->addForce(wind);
- for (auto part:this->particles)
- {
- part->addForce(wind);
- part->integrate(deltaTime);
- // check the particles position and keep the particule in the window.
- if ( ((part->position.y - part->radius) <= 0) ||
- ((part->position.y + part->radius) >= graphics::height()))
- {
- part->velocity.y = -part->velocity.y;
- }
- if ( ((part->position.x - part->radius) <= 0) ||
- ((part->position.x + part->radius) >= graphics::width()))
- {
- part->velocity.x = -part->velocity.x;
- }
- }
- if ( ((this->part->position.x - this->part->radius) <= 0) ||
- ((this->part->position.x + this->part->radius) >= graphics::width()))
- {
- this->part->velocity.x = -this->part->velocity.x;
- }
- }
- void application::render()
- {
- graphics::clearScreen(0xFF056263);
- for (auto part:this->particles)
- {
- graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFFFFFFF);
- }
- graphics::draw::fillCircle(this->part->position.x, this->part->position.y, 4, 0xFFFFFFFF);
- graphics::renderFrame();
- }
- void application::destroy()
- {
- // Nothing for now.
- for (auto part:this->particles)
- {
- delete part;
- }
- graphics::closeWindow();
- }
|