From 7575e86e469d242860907aa3b05daa5ea44626b2 Mon Sep 17 00:00:00 2001 From: Siddharth Roy Date: Wed, 22 Nov 2023 22:17:31 +0530 Subject: [PATCH] Simplified example --- src/app.cpp | 35 ++++++++++++++++++++++++++++++++ src/app.hpp | 13 ++++++++++++ src/main.cpp | 54 ------------------------------------------------- src/wrapper.cpp | 41 +++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 54 deletions(-) create mode 100644 src/app.cpp create mode 100644 src/app.hpp delete mode 100644 src/main.cpp create mode 100644 src/wrapper.cpp diff --git a/src/app.cpp b/src/app.cpp new file mode 100644 index 0000000..bc02a8a --- /dev/null +++ b/src/app.cpp @@ -0,0 +1,35 @@ +#include "app.hpp" +#include "../libs/raylib/src/raylib.h" +#include "helper.hpp" + +Texture2D texture; + +void init_app() +{ + texture = LoadTexture("assets/test.png"); +} + +bool app_loop() +{ + + BeginDrawing(); + + ClearBackground(RAYWHITE); + + const int texture_x = getWindowWidth() / 2 - texture.width / 2; + const int texture_y = getWindowHeight() / 2 - texture.height / 2; + DrawTexture(texture, texture_x, texture_y, WHITE); + + const char *text = "OMG! IT WORKS!"; + const Vector2 text_size = MeasureTextEx(GetFontDefault(), text, 20, 1); + DrawText(text, getWindowWidth() / 2.0 - text_size.x / 2, texture_y + texture.height + text_size.y + 10, 20, BLACK); + + EndDrawing(); + + return true; +} + +void deinit_app() +{ + UnloadTexture(texture); +} \ No newline at end of file diff --git a/src/app.hpp b/src/app.hpp new file mode 100644 index 0000000..4763015 --- /dev/null +++ b/src/app.hpp @@ -0,0 +1,13 @@ +#ifndef APP_HPP +#define APP_HPP + +// Load asset and initialized stuffs here +void init_app(); + +// The main loop (return false to end loop) +bool app_loop(); + +// Free up allocated memory +void deinit_app(); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 7ad5bef..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "../libs/raylib/src/raylib.h" -#include "helper.hpp" - -Texture2D texture; - -void loop() { -#if defined(PLATFORM_WEB) - static int old_w=0,old_h=0; - - int w = getWindowWidth(); - int h = getWindowHeight(); - if(w!=old_w || h!=old_h){ SetWindowSize(w,h); } -#endif - - BeginDrawing(); - - ClearBackground(RAYWHITE); - - const int texture_x = getWindowWidth() / 2 - texture.width / 2; - const int texture_y = getWindowHeight()/ 2 - texture.height / 2; - DrawTexture(texture, texture_x, texture_y, WHITE); - - const char* text = "OMG! IT WORKS!"; - const Vector2 text_size = MeasureTextEx(GetFontDefault(), text, 20, 1); - DrawText(text, getWindowWidth() / 2.0 - text_size.x / 2, texture_y + texture.height + text_size.y + 10, 20, BLACK); - - EndDrawing(); - -} - -int main(void) { - // Setup window -#if defined(PLATFORM_WEB) - InitWindow(getBrowserWindowWidth(), getBrowserWindowHeight(), PROJECT_NAME); -#else - InitWindow(800, 500, PROJECT_NAME); -#endif - SetWindowState(FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_MAXIMIZED); - - // Setup assets - texture = LoadTexture("assets/test.png"); - -#if defined(PLATFORM_WEB) - emscripten_set_main_loop(loop, 0, 1); -#else - while (!WindowShouldClose()) { - loop(); - } -#endif - - // Cleanup - CloseWindow(); - return 0; -} diff --git a/src/wrapper.cpp b/src/wrapper.cpp new file mode 100644 index 0000000..af5cb8d --- /dev/null +++ b/src/wrapper.cpp @@ -0,0 +1,41 @@ +#include "../libs/raylib/src/raylib.h" +#include "helper.hpp" +#include "app.hpp" + +void web_loop() +{ + static int old_w = 0, old_h = 0; + + int w = getWindowWidth(); + int h = getWindowHeight(); + if (w != old_w || h != old_h) + { + SetWindowSize(w, h); + } + app_loop(); +} + +int main(void) +{ + // Setup window +#if defined(PLATFORM_WEB) + InitWindow(getBrowserWindowWidth(), getBrowserWindowHeight(), PROJECT_NAME); +#else + InitWindow(800, 500, PROJECT_NAME); +#endif + SetWindowState(FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_MAXIMIZED); + init_app(); + +#if defined(PLATFORM_WEB) + emscripten_set_main_loop(web_loop, 0, 1); +#else + while (app_loop() && !WindowShouldClose()) + { + } +#endif + deinit_app(); + + // Cleanup + CloseWindow(); + return 0; +}