Created
October 1, 2026 16:22
-
-
Save stungeye/81999de13fb4d1c497a07590d0ce5223 to your computer and use it in GitHub Desktop.
C++ Classes with Raylib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "ball.hpp" | |
| #include <algorithm> | |
| Ball::Ball(int x, int y, float radius, Color color, int speedX, int speedY) | |
| : x{ x }, y{ y }, radius{ radius }, color{ color }, speedX{ speedX }, speedY{ speedY } | |
| { } | |
| void Ball::show() { | |
| DrawEllipse(x, y, radius, radius, color); | |
| } | |
| void Ball::move() { | |
| x += speedX; | |
| y += speedY; | |
| bounceOnEdge(); | |
| } | |
| void Ball::bounceOnEdge() { | |
| const int r{ static_cast<int>(radius) }; | |
| const int xMax{ GetScreenWidth() - r }; | |
| const int yMax{ GetScreenHeight() - r }; | |
| if (x < r || x > xMax) { | |
| speedX *= -1; | |
| } | |
| if (y < r || y > yMax) { | |
| speedY *= -1; | |
| } | |
| x = std::clamp(x, r, xMax); | |
| y = std::clamp(y, r, yMax); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "raylib.h" | |
| class Ball { | |
| // Private Members | |
| int x; | |
| int y; | |
| float radius; | |
| Color color; | |
| int speedX; | |
| int speedY; | |
| public: | |
| Ball(int x, int y, float radius, Color color, int speedX, int speedY); | |
| void show(); | |
| void move(); | |
| private: | |
| void bounceOnEdge(); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "raylib.h" | |
| #include "ball.hpp" | |
| #include "resource_dir.h" // utility header for SearchAndSetResourceDir | |
| #include <vector> | |
| int main () { | |
| constexpr size_t numberOfBalls{ 500 }; | |
| std::vector<Ball> ballCollection; | |
| // Tell the window to use vsync and work on high DPI displays | |
| SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI); | |
| // Create the window and OpenGL context | |
| InitWindow(1500, 800, "Hello Raylib"); | |
| // Utility function from resource_dir.h to find the resources folder and set it as the current working directory so we can load from it | |
| SearchAndSetResourceDir("resources"); | |
| // Load a texture from the resources directory | |
| Texture wabbit = LoadTexture("wabbit_alpha.png"); | |
| for (size_t i{ 0 }; i < numberOfBalls; i++) { | |
| ballCollection.push_back( | |
| Ball{ | |
| GetRandomValue(0, GetScreenWidth()), | |
| GetRandomValue(0, GetScreenHeight()), | |
| static_cast<float>(GetRandomValue(0, 80)), | |
| Color{ (unsigned char)GetRandomValue(0, 255), (unsigned char)GetRandomValue(0, 255), (unsigned char)GetRandomValue(0, 255), 255 }, | |
| GetRandomValue(-5, 5), | |
| GetRandomValue(-5, 5) | |
| } | |
| ); | |
| } | |
| // game loop | |
| while (!WindowShouldClose()) // run the loop until the user presses ESCAPE or presses the Close button on the window | |
| { | |
| // drawing | |
| BeginDrawing(); | |
| // Setup the back buffer for drawing (clear color and depth buffers) | |
| ClearBackground(BLACK); | |
| // draw some text using the default font | |
| DrawText("Hello Raylib", 200,200,20,WHITE); | |
| for (Ball& ball : ballCollection) { | |
| ball.move(); | |
| ball.show(); | |
| } | |
| // draw our texture to the screen | |
| DrawTexture(wabbit, 400, 200, WHITE); | |
| // end the frame and get ready for the next one (display frame, poll input, etc...) | |
| EndDrawing(); | |
| } | |
| // cleanup | |
| // unload our texture so it can be cleaned up | |
| UnloadTexture(wabbit); | |
| // destroy the window and cleanup the OpenGL context | |
| CloseWindow(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment