Skip to content

Commit

Permalink
Simplified example
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthroy12 committed Nov 22, 2023
1 parent edd9f6f commit 7575e86
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 54 deletions.
35 changes: 35 additions & 0 deletions src/app.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
13 changes: 13 additions & 0 deletions src/app.hpp
Original file line number Diff line number Diff line change
@@ -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
54 changes: 0 additions & 54 deletions src/main.cpp

This file was deleted.

41 changes: 41 additions & 0 deletions src/wrapper.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7575e86

Please sign in to comment.