This commit is contained in:
Timerix 2024-08-08 18:03:20 +03:00
parent b8f041584f
commit 0aefa70fb8
9 changed files with 65 additions and 7 deletions

View File

@ -164,7 +164,12 @@ void MainWindow::draw_frame(){
//Resources::Texture tutel(Resources::getResource("tutel.png"), sdl_renderer); //Resources::Texture tutel(Resources::getResource("tutel.png"), sdl_renderer);
static Resources::CacheStorage<Resources::Texture> textures; static Resources::CacheStorage<Resources::Texture> textures;
Resources::Texture& tutel = textures.getOrCreate("tutel.png", sdl_renderer); Resources::Texture& tutel = textures.getOrCreate("tutel.png", sdl_renderer);
tutel.render(SDL_FRectConstruct(100, 100, 400, 400)); Resources::SDL_RenderCopyExF_Params rp;
rp.target_section = SDL_FRectConstruct(100, 100, 400, 400);
static int si = 1;
rp.rotation_angle = M_PI_4 * (si++ / fps_max);
tutel.render(rp);
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer); ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer);
// Swap buffers // Swap buffers
@ -193,7 +198,6 @@ void MainWindow::startUpdateLoop(){
nsec_t after_update_time_ns = getMonotonicTimeNsec(); nsec_t after_update_time_ns = getMonotonicTimeNsec();
nsec_t frame_delay_ns = (nsec_t)1e9 / fps_max - (after_update_time_ns - update_time_ns); nsec_t frame_delay_ns = (nsec_t)1e9 / fps_max - (after_update_time_ns - update_time_ns);
if(frame_delay_ns > 0){ if(frame_delay_ns > 0){
std::cout<<"frameDelay: "<<frame_delay_ns / 1e9f<<std::endl;
SDL_Delay(frame_delay_ns / 1e6); SDL_Delay(frame_delay_ns / 1e6);
} }
} }

View File

@ -20,7 +20,7 @@ class MainWindow {
public: public:
ImVec4 clear_color = RGBAHexToF(35,35,50,255); ImVec4 clear_color = RGBAHexToF(35,35,50,255);
f32 default_font_size = 14.0f; f32 default_font_size = 14.0f;
int fps_max = 30; int fps_max = 60;
// called on each frame // called on each frame
UpdatingFunc update = nullptr; UpdatingFunc update = nullptr;

0
src/GUI/SceneView.hpp Normal file
View File

15
src/Game/GameObject.hpp Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include "../math.hpp"
#include <set>
class GameObject {
public:
Vec2 scale = { 1, 1 };
Vec2 position;
angle_t rotation;
private:
// std::set<Component> components;
};

7
src/Game/Scene.hpp Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include "GameObject.hpp"
class Scene {
};

View File

@ -41,7 +41,7 @@ void Texture::render(const SDL_RenderCopyExF_Params& p){
SDL_RenderCopyExF(renderer, texture.get(), SDL_RenderCopyExF(renderer, texture.get(),
optional_value_ptr_or_null(p.texture_section), optional_value_ptr_or_null(p.texture_section),
optional_value_ptr_or_null(p.target_section), optional_value_ptr_or_null(p.target_section),
p.rotation_angle, -1.0f * angleToDegree(normalizeAngle(p.rotation_angle)),
optional_value_ptr_or_null(p.rotation_center), optional_value_ptr_or_null(p.rotation_center),
p.flip p.flip
) )

View File

@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <SDL.h> #include <SDL.h>
#include "Resources.hpp" #include "Resources.hpp"
#include "../math.hpp"
namespace ougge::Resources { namespace ougge::Resources {
@ -22,7 +23,7 @@ struct SDL_RenderCopyExF_Params {
std::optional<SDL_Rect> texture_section; std::optional<SDL_Rect> texture_section;
std::optional<SDL_FRect> target_section; std::optional<SDL_FRect> target_section;
std::optional<SDL_FPoint> rotation_center; std::optional<SDL_FPoint> rotation_center;
double rotation_angle; angle_t rotation_angle;
SDL_RendererFlip flip; SDL_RendererFlip flip;
SDL_RenderCopyExF_Params(); SDL_RenderCopyExF_Params();

View File

@ -1,15 +1,15 @@
#define SDL_MAIN_HANDLED #define SDL_MAIN_HANDLED
#include <iostream> #include <iostream>
#include "std.hpp"
#include "GUI/MainWindow.hpp" #include "GUI/MainWindow.hpp"
#include "Resources/Resources.hpp" #include "Resources/Resources.hpp"
#include "Game/Scene.hpp"
#include "format.hpp" #include "format.hpp"
#include "exceptions.hpp" #include "exceptions.hpp"
using namespace ougge; using namespace ougge;
void update(f64 deltaTime){ void update(f64 deltaTime){
std::cout<<"deltaTime: "<<deltaTime<<std::endl; // std::cout<<"deltaTime: "<<deltaTime<<std::endl;
} }
int main(int argc, const char** argv){ int main(int argc, const char** argv){

31
src/math.hpp Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include "std.hpp"
#include <cmath>
struct Vec2 {
f32 x = 0, y = 0;
};
struct Vec3 {
f32 x = 0, y = 0, z = 0;
};
struct Vec4 {
f32 x = 0, y = 0, z = 0, w = 0;
};
/// radians
typedef f32 angle_t;
static inline angle_t normalizeAngle(angle_t a){
return std::fmodf(a + M_PI, 2*M_PI) - M_PI;
}
static inline f32 angleToDegree(angle_t a){
return (a / M_PI) * 180;
}
static inline angle_t degreeToRadian(f32 d){
return (d / 180) * M_PI;
}