From 39da1fac6eed3e8e9636e48024fba735c2a404bb Mon Sep 17 00:00:00 2001 From: dreat Date: Mon, 3 Jun 2024 14:30:56 +0200 Subject: [PATCH] raylib stuff --- _notes/Making a game with C++ and Raylib.md | 9 ++ _notes/learning.md | 3 +- _notes/make setup.md | 102 ++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 _notes/Making a game with C++ and Raylib.md create mode 100644 _notes/make setup.md diff --git a/_notes/Making a game with C++ and Raylib.md b/_notes/Making a game with C++ and Raylib.md new file mode 100644 index 0000000..5ca207b --- /dev/null +++ b/_notes/Making a game with C++ and Raylib.md @@ -0,0 +1,9 @@ +Status: 🌱 + +A bit of context: + +I gotten my hands on a course that was intro to writing games with C++. While it wasn't perfect, I got some starting point and inspiration - Raylib seems low level enough to get me with "all the fun", but high enough so I don't need to manually manage all OpenGL, which felt a bit too much at a time. + +I'm documenting my journey, all with wins, fails and lessons I got along the way. + +1. [[make setup]] \ No newline at end of file diff --git a/_notes/learning.md b/_notes/learning.md index 0c2f2ab..c6b25f4 100644 --- a/_notes/learning.md +++ b/_notes/learning.md @@ -6,4 +6,5 @@ title: Learning Here is a list of my notes done during learning various things -[[Korean]] \ No newline at end of file +[[Korean]] +[[Making a game with C++ and Raylib]] \ No newline at end of file diff --git a/_notes/make setup.md b/_notes/make setup.md new file mode 100644 index 0000000..ca417eb --- /dev/null +++ b/_notes/make setup.md @@ -0,0 +1,102 @@ +Status: 🌿 + +Because I'm a weirdo, I decided to go with terminal only development. That means no `easy mode` of going with VSCode and pressing `build` to make it work. + +But it turns out (at least for start) it's not as difficult to do: + +### Step one - Install Raylib + +I'm on MacOS, so I just typed `brew install raylib`. You can easily get it on official website as well - https://www.raylib.com + +### Step two - Prepare build + +I went with a very simple Makefile, that I prepared during the course and was using for all projects there + +```bash +.Phony: format + +files := main.cpp + +build: $(files) + clang++ -o game $(files) -Wall -std=c++14 -g -O0 -I. -I/opt/homebrew/include -L. -L/opt/homebrew/lib -lraylib -framework OpenGL + +format: + clang-format -i *.cpp + clang-format -i *.h + +``` + +(You will need clang-format to use `format` target). + +Now, when I type `make`, my code is compiled and I can type `./game` to mess around. +When I type `make format` my code get's formatted. + +For a brief explanation what's happening here: + +- This is a Makefile, which `tldr` "helps building software" +- `.Phony:` is used to say "hey, run it always" +- `files := main.cpp` is where you define files for your c++ project +- `clang++ ...` is the compiling step + - output `game` executable, so it's possible to run `./game` and mess around + - show warnings, use c++14, all that jazz + - include all files from current directory as well as `/opt/homebrew/include` + - link all libs from current directory as well as `opt/homebrew/lib` + - (if brew was not used to install raylib, obviously the paths will change) + - `I want raylib` + - `use OpenGl` +- and `format` -> with `make format` I get formatted code, `clang-format` is needed, obviously + + +### Step three - render something + +With following `main.cpp` + +```cpp +#include "raylib.h" + +int main() { + const int windowWidth = 300; + const int windowHeight = 300; + InitWindow(windowWidth, windowHeight, "Hello Raylib"); + + unsigned char red = 0; + unsigned char green = 117; + unsigned char blue = 44; + Color color = {}; + + SetTargetFPS(60); + while (!WindowShouldClose()) { + red = (red + 1) % 128; + green = (green + 2) % 128 + 128; + blue = (blue + 3) % 128; + color = {red, green, blue, 255}; + BeginDrawing(); + ClearBackground(WHITE); + DrawCircle(windowWidth / 2, windowWidth / 2, 50, color); + + EndDrawing(); + } + CloseWindow(); + return 0; +} + +``` + +Code is not pretty, but will get the job done. +So; what's happening here? + +1. Creating window. Width, height and title. +2. Then, I decided to be a bit fancy and (had to look into Raylib cheatsheet) created manually a color that I will manipulate. + - Yes, those are unsigned chars, probably not most convenient way of doing that + - `Color color = {};` seems like pretty recent addition - simple, but default initialisation is something I really enjoy +4. `SetTargetFPS(60)` set's the maximum FPS the game will run. +5. `while` loop with check if `esc` was pressed or `close window` button was pressed. Pretty handy, thou for game I would have not just kill everything on simple `esc` press. +6. Do wonky color manipulation. +7. `BeginDrawing()` and `EndDrawing()` are required, to easily show what goes into a frame. +8. `ClearBackground(WHITE);` + - `WHITE` is Raylib's built in `Color` that we can use. + - this method is used to prevent screen flickering +9. Draw our circle in the middle of the screen. Left right edge is {0,0} and it increases down and right. We give it `color`, so it is `animated`. +10. Once we're out of the loop, close window and be on own's merry way. + +That should give a solid headstart for some simple experimentation with Raylib - if you know how to code it should be fun exploring how this can be extended. \ No newline at end of file