6 Commits

Author SHA1 Message Date
1ceab525b9 Change background color from GREEN to BLACK 2025-12-09 14:15:32 -06:00
Maple Redleaf
694411efdf Add DRM rendering & clean up 2025-12-09 14:13:36 -06:00
maple
f97e8fe9d8 Begin engine development 2025-11-10 01:58:47 +00:00
Maple Redleaf
99275870eb Finished separating MainLoop 2025-11-07 13:12:55 -06:00
maple
56a1f3c1da Update Readme 2025-11-07 12:42:48 +00:00
maple
d7ea07eb85 Begin restructuring functions 2025-11-07 12:15:00 +00:00
2 changed files with 48 additions and 68 deletions

View File

@@ -1,42 +1,3 @@
# Simple and portable CMake template for raylib # RaylibGame
This is a basic project template for raylib using CMake and has been tested with Visual Studio, Visual Studio Code and CLion. A template for making games with Raylib-CPP
The master branch of the raylib source code is downloaded using CMake FetchContent from github and compiled from source as it is much easier than including prebuilt binaries for every platform and configuration.
Building from the cmake file will build both raylib and `src/main.c` which includes a basic example of a raylib program.
## Asset handling
The example in `src/main.c` uses an example image located in the `assets` folder.
To load it we use `ASSETS_PATH`, which is a string macro with the *absolute* path "assets" directory.
This macro is defined in the `CMakeLists.txt` file on line `23`.
If you plan on releasing or sharing your game consider manually setting the value of the `ASSETS_PATH` macro.
In C you can concatenate string literals by putting them next to each other,
eg: `"A" "B"` is `"AB"`. So ASSETS_PATH `"test.png"` becomes `"/path/to/your/assets/test.png"`
If you wanna share your game with others you should set ASSETS_PATH to be a *relative* path like "./assets/". You can do this in the CMakeLists.txt file.
## Using C++
Using c++ is quite simple, just change these lines in the `CMakeLists.txt`
from
```
project(my_raylib_game C)
set(CMAKE_C_STANDARD 99)
file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/sources/*.c")
```
to
```
project(my_raylib_game CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/sources/*.cpp")
```
After this just reload cmake and it should build fine.

View File

@@ -3,39 +3,62 @@
#include <cmath> #include <cmath>
#include <raylib-cpp.hpp> #include <raylib-cpp.hpp>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#define SCREEN_WIDTH (320) #define SCREEN_WIDTH (320)
#define SCREEN_HEIGHT (240) #define SCREEN_HEIGHT (240)
#define WIN_WIDTH (1280)
#define WIN_HEIGHT (720)
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
void MainLoop();
raylib::Window window(WIN_WIDTH, WIN_HEIGHT, "game",
FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
raylib::RenderTexture2D target(SCREEN_WIDTH, SCREEN_HEIGHT);
raylib::Image texImg =
raylib::LoadImageFromMemory(".png", cube_png, cube_png_len);
raylib::Texture2D texture = texImg.LoadTexture();
raylib::Camera3D cam(raylib::Vector3(0.0f, 0.0f, std::sqrt(3.0f)),
raylib::Vector3::Zero(), raylib::Vector3(0.0f, 1.0f, 0.0f),
60.0f, CAMERA_PERSPECTIVE);
raylib::Model cube(GenMeshCube(1.0f, 1.0f, 1.0f));
int main(void) { int main(void) {
raylib::Window window(SCREEN_WIDTH, SCREEN_HEIGHT, "game",
FLAG_MSAA_4X_HINT | FLAG_WINDOW_RESIZABLE |
FLAG_WINDOW_MAXIMIZED | FLAG_VSYNC_HINT);
window.SetMinSize({320, 240}); window.SetMinSize({320, 240});
window.SetTargetFPS(60); window.SetTargetFPS(60);
window.SetExitKey(KEY_BACKSPACE); window.SetExitKey(KEY_BACKSPACE);
raylib::RenderTexture2D target(320, 240);
target.GetTexture().SetFilter(TEXTURE_FILTER_POINT); target.GetTexture().SetFilter(TEXTURE_FILTER_POINT);
raylib::Image texImg =
raylib::LoadImageFromMemory(".png", cube_png, cube_png_len);
raylib::Texture2D texture = texImg.LoadTexture();
raylib::Camera3D cam(raylib::Vector3(0.0, 0.0, std::sqrt(3.0)),
raylib::Vector3::Zero(), {0.0f, 1.0f, 0.0f}, 60.0f,
CAMERA_PERSPECTIVE);
raylib::Model cube(GenMeshCube(1.0f, 1.0f, 1.0f));
cube.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = texture; cube.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = texture;
while (!window.ShouldClose()) { #if defined(PLATFORM_WEB)
emscripten_set_main_loop(MainLoop, 0, 1);
#else
while (!window.ShouldClose() && !raylib::Keyboard::IsKeyDown(KEY_ESCAPE))
MainLoop();
#endif
float scale = MIN((float)window.GetWidth() / SCREEN_WIDTH, window.Close();
(float)window.GetHeight() / SCREEN_HEIGHT);
return 0;
}
void MainLoop() {
{
int winWidth = GetScreenWidth(), winHeight = GetScreenHeight();
float scale =
MIN((float)winWidth / SCREEN_WIDTH, (float)winHeight / SCREEN_HEIGHT);
cube.SetTransform( cube.SetTransform(
raylib::Matrix::RotateXYZ( raylib::Matrix::RotateXYZ(
@@ -59,18 +82,14 @@ int main(void) {
{ {
ClearBackground(BLACK); ClearBackground(BLACK);
target.GetTexture().Draw( target.GetTexture().Draw(
raylib::Rectangle(0.0f, 0.0f, (float)target.GetTexture().width, raylib::Rectangle(0.0f, 0.0f, (float)SCREEN_WIDTH,
(float)-target.GetTexture().height), (float)-SCREEN_HEIGHT),
raylib::Rectangle( raylib::Rectangle((winWidth - ((float)SCREEN_WIDTH * scale)) * 0.5f,
(window.GetWidth() - ((float)SCREEN_WIDTH * scale)) * 0.5f, (winHeight - ((float)SCREEN_HEIGHT * scale)) * 0.5f,
(window.GetHeight() - ((float)SCREEN_HEIGHT * scale)) * 0.5f, (float)SCREEN_WIDTH * scale,
(float)SCREEN_WIDTH * scale, (float)SCREEN_HEIGHT * scale), (float)SCREEN_HEIGHT * scale),
raylib::Vector2(0, 0), 0.0f, WHITE); raylib::Vector2(0, 0), 0.0f, WHITE);
} }
window.EndDrawing(); window.EndDrawing();
} }
window.Close();
return 0;
} }