mirror of
https://github.com/NickTheFox99/RaylibGame.git
synced 2026-06-05 09:00:52 -04:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1ceab525b9 | |||
|
|
694411efdf | ||
|
|
f97e8fe9d8 | ||
|
|
99275870eb | ||
|
|
56a1f3c1da | ||
|
|
d7ea07eb85 |
43
README.md
43
README.md
@@ -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.
|
|
||||||
|
|||||||
73
src/main.cpp
73
src/main.cpp
@@ -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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user