7
0
mirror of https://gitlab.com/raylibtemplates/rt.git synced 2026-06-10 02:35:11 -04:00

11 Commits

Author SHA1 Message Date
Maple Redleaf
651fb47cd4 Reimplement C++ into C 2025-12-19 11:53:51 -06:00
Maple Redleaf
4ec432602f Fix variable names for cube data 2025-12-19 11:27:37 -06:00
Maple Redleaf
c7099c5b1d Revert to last C version 2025-12-19 11:20:02 -06:00
Maple Redleaf
093eb8388f Remove xxd build step from CMakeLists.txt
Asset headers should be in the repository instead of built now
2025-12-19 11:10:51 -06:00
Maple Redleaf
b1f494c5d8 Remove CPP dependencies from CMakeLists.txt 2025-12-19 11:03:52 -06:00
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
6 changed files with 113 additions and 176 deletions

4
.gitignore vendored
View File

@@ -1,4 +1,2 @@
build/* build*/*
build-web/*
data/*
.cache/* .cache/*

View File

@@ -21,75 +21,21 @@ if(NOT raylib_FOUND) # If there's none, fetch and build raylib
endif() endif()
endif() endif()
set(RAYLIB_CPP_VERSION "v5.5.0")
find_package(raylib_cpp QUIET) # QUIET or REQUIRED
if(NOT raylib_cpp_FOUND) # If there's none, fetch and build raylib
include(FetchContent)
FetchContent_Declare(
raylib_cpp
DOWNLOAD_EXTRACT_TIMESTAMP OFF
URL https://github.com/RobLoach/raylib-cpp/archive/refs/tags/${RAYLIB_CPP_VERSION}.tar.gz
)
FetchContent_GetProperties(raylib_cpp)
if(NOT raylib_cpp_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_MakeAvailable(raylib_cpp)
endif()
endif()
# Our Project # Our Project
add_executable(${PROJECT_NAME}) add_executable(${PROJECT_NAME})
# Generate header files from assets using xxd
file(GLOB_RECURSE ASSET_FILES ${CMAKE_CURRENT_SOURCE_DIR}/assets/*)
set(DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/data)
set(GENERATED_HEADERS "")
foreach(ASSET_FILE ${ASSET_FILES})
file(RELATIVE_PATH REL_PATH
${CMAKE_CURRENT_SOURCE_DIR}/assets
${ASSET_FILE}
)
get_filename_component(FILE_NAME ${ASSET_FILE} NAME)
get_filename_component(SUB_DIR ${REL_PATH} DIRECTORY)
set(OUTPUT_DIR ${DATA_DIR}/${SUB_DIR})
file(MAKE_DIRECTORY ${OUTPUT_DIR})
set(OUTPUT_FILE ${OUTPUT_DIR}/${FILE_NAME}.h)
get_filename_component(ASSET_DIR ${ASSET_FILE} DIRECTORY)
add_custom_command(
OUTPUT ${OUTPUT_FILE}
COMMAND xxd -i ${FILE_NAME} ${OUTPUT_FILE}
DEPENDS ${ASSET_FILE}
WORKING_DIRECTORY ${ASSET_DIR}
COMMENT "Embedding ${REL_PATH}"
VERBATIM
)
list(APPEND GENERATED_HEADERS ${OUTPUT_FILE})
endforeach()
# Add source files # Add source files
file(GLOB_RECURSE PROJECTSOURCES "src/*.c" "src/*.cpp") file(GLOB_RECURSE PROJECTSOURCES "src/*.c" "src/*.cpp")
target_sources(${PROJECT_NAME} PRIVATE ${PROJECTSOURCES}) target_sources(${PROJECT_NAME} PRIVATE ${PROJECTSOURCES})
# Add generated headers
target_sources(${PROJECT_NAME} PRIVATE ${GENERATED_HEADERS})
# Include directories # Include directories
target_include_directories(${PROJECT_NAME} PRIVATE target_include_directories(${PROJECT_NAME} PRIVATE
${DATA_DIR} "data"
${raylib_INCLUDE_DIRS} ${raylib_INCLUDE_DIRS}
${raylib_cpp_INCLUDE_DIRS}
) )
# Link libraries # Link libraries
target_link_libraries(${PROJECT_NAME} raylib raylib_cpp) target_link_libraries(${PROJECT_NAME} raylib)
# Web Configurations # Web Configurations
if(${PLATFORM} STREQUAL "Web") if(${PLATFORM} STREQUAL "Web")

View File

@@ -1,42 +1,3 @@
# Simple and portable CMake template for raylib # RaylibTemplate
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 (with CMake)
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.

11
data/cube.png.h Normal file
View File

@@ -0,0 +1,11 @@
unsigned char cube_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08,
0x08, 0x06, 0x00, 0x00, 0x00, 0xc4, 0x0f, 0xbe, 0x8b, 0x00, 0x00, 0x00,
0x2b, 0x49, 0x44, 0x41, 0x54, 0x18, 0x57, 0x63, 0x64, 0x60, 0x60, 0xf8,
0xcf, 0x80, 0x07, 0x30, 0x82, 0x14, 0xd4, 0x33, 0x30, 0x30, 0x34, 0x60,
0xc1, 0x20, 0x49, 0xb0, 0x02, 0x90, 0x11, 0xe8, 0x92, 0x20, 0xdc, 0x38,
0x98, 0x4c, 0x80, 0xfb, 0x09, 0x0b, 0x00, 0x00, 0x41, 0x79, 0x1d, 0x01,
0xd9, 0xf2, 0x1b, 0x87, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44,
0xae, 0x42, 0x60, 0x82, 0x00, 0x00};
unsigned int cube_png_len = 102;

97
src/main.c Normal file
View File

@@ -0,0 +1,97 @@
#include "raylib.h"
#include "raymath.h"
#include <math.h>
#include <stdio.h>
#include "../data/cube.png.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#define SCREEN_WIDTH (320)
#define SCREEN_HEIGHT (240)
#define WIN_WIDTH (1280)
#define WIN_HEIGHT (720)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void MainLoop();
RenderTexture2D target;
Image texImg;
Texture2D texture;
Camera3D cam;
Model cube;
int main(void) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
InitWindow(WIN_WIDTH, WIN_HEIGHT, "game");
SetTargetFPS(60);
SetExitKey(KEY_BACKSPACE);
target = LoadRenderTexture(SCREEN_WIDTH, SCREEN_HEIGHT);
SetTextureFilter(target.texture, TEXTURE_FILTER_POINT);
texImg = LoadImageFromMemory(".png", cube_png, cube_png_len);
texture = LoadTextureFromImage(texImg);
cam = (Camera3D){0};
cam.position = (Vector3){0.0f, 0.0f, sqrtf(3.0f)};
cam.target = (Vector3){0.0f, 0.0f, 0.0f};
cam.up = (Vector3){0.0f, 1.0f, 0.0f};
cam.fovy = 60.0f;
cam.projection = CAMERA_PERSPECTIVE;
cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
cube.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = texture;
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(MainLoop, 0, 1);
#else
while (!WindowShouldClose())
MainLoop();
#endif
CloseWindow();
return 0;
}
void MainLoop() {
int winWidth = GetScreenWidth(), winHeight = GetScreenHeight();
float scale =
MIN((float)winWidth / SCREEN_WIDTH, (float)winHeight / SCREEN_HEIGHT);
cube.transform =
MatrixMultiply(MatrixRotateXYZ(Vector3Scale((Vector3){1.0f, 1.5f, 2.5f},
GetFrameTime())),
cube.transform);
BeginTextureMode(target);
{
ClearBackground(BLACK);
DrawCircleV((Vector2){160.0f, 120.0f}, 120.0f, WHITE);
BeginMode3D(cam);
{
DrawModel(cube, Vector3Zero(), 1.0f, WHITE);
}
EndMode3D();
}
EndTextureMode();
BeginDrawing();
{
ClearBackground(BLACK);
DrawTexturePro(
target.texture,
(Rectangle){0.0f, 0.0f, (float)SCREEN_WIDTH, (float)-SCREEN_HEIGHT},
(Rectangle){(winWidth - ((float)SCREEN_WIDTH * scale)) * 0.5f,
(winHeight - ((float)SCREEN_HEIGHT * scale)) * 0.5f,
(float)SCREEN_WIDTH * scale, (float)SCREEN_HEIGHT * scale},
(Vector2){0, 0}, 0.0f, WHITE);
}
EndDrawing();
}

View File

@@ -1,76 +0,0 @@
#include "../data/cube.png.h"
#include "raylib.h"
#include <cmath>
#include <raylib-cpp.hpp>
#define SCREEN_WIDTH (320)
#define SCREEN_HEIGHT (240)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
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.SetTargetFPS(60);
window.SetExitKey(KEY_BACKSPACE);
raylib::RenderTexture2D target(320, 240);
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;
while (!window.ShouldClose()) {
float scale = MIN((float)window.GetWidth() / SCREEN_WIDTH,
(float)window.GetHeight() / SCREEN_HEIGHT);
cube.SetTransform(
raylib::Matrix::RotateXYZ(
raylib::Vector3(1.0f, 1.5f, 2.5f).Scale(window.GetFrameTime()))
.Multiply(cube.GetTransform()));
target.BeginMode();
{
ClearBackground(BLACK);
raylib::Vector2(160.0f, 120.0f)
.DrawCircle(120.0f, raylib::Color::White());
cam.BeginMode();
{
cube.Draw(raylib::Vector3::Zero());
}
cam.EndMode();
}
target.EndMode();
window.BeginDrawing();
{
ClearBackground(BLACK);
target.GetTexture().Draw(
raylib::Rectangle(0.0f, 0.0f, (float)target.GetTexture().width,
(float)-target.GetTexture().height),
raylib::Rectangle(
(window.GetWidth() - ((float)SCREEN_WIDTH * scale)) * 0.5f,
(window.GetHeight() - ((float)SCREEN_HEIGHT * scale)) * 0.5f,
(float)SCREEN_WIDTH * scale, (float)SCREEN_HEIGHT * scale),
raylib::Vector2(0, 0), 0.0f, WHITE);
}
window.EndDrawing();
}
window.Close();
return 0;
}