18 Commits

Author SHA1 Message Date
Maple Redleaf
6b1e840810 Add light spread 2025-12-18 14:33:29 -06:00
Maple Redleaf
5f079d45a3 Resize game for better visual clarity / looks 2025-12-18 12:31:00 -06:00
Maple Redleaf
992907dfad Add Lights Manager, set up Lights, and add Tree. 2025-12-18 12:24:49 -06:00
Maple Redleaf
61168bee04 Fix lights cycle 2025-12-18 11:00:26 -06:00
Maple Redleaf
fd7d44e2ce Finish creating singular lights 2025-12-18 07:37:02 -06:00
Maple
f01cbc782f Begin work on lights 2025-12-15 06:01:58 -06:00
Luke W.
5e2451459c Fixed copy init of sMgr 2025-12-12 16:57:50 -06:00
Maple Redleaf
b2d7d71c80 Fix pointers 2025-12-12 16:47:51 -06:00
Maple Redleaf
cf602bb513 Fix types 2025-12-12 12:28:57 -06:00
Maple Redleaf
2c15d64ab3 Fix snow instantiation & position 2025-12-12 11:47:56 -06:00
Maple Redleaf
795b54a90a Update CMakeLists.txt 2025-12-12 11:19:05 -06:00
Maple Redleaf
99f769a79b Add rudimentary implementation of Snow::Manager 2025-12-12 10:49:18 -06:00
Maple Redleaf
fabf4ea2d1 Fix graphics error 2025-12-12 10:18:20 -06:00
Maple Redleaf
588d921553 Finish Snow::Manager implementation 2025-12-12 07:40:19 -06:00
Maple Redleaf
9f4b0015aa Begin work on Snow::Manager 2025-12-11 15:31:52 -06:00
Maple Redleaf
85bfe365ce Split snow into namespace 2025-12-11 14:08:23 -06:00
Maple Redleaf
e8e6768ca4 Fix includes 2025-12-11 13:55:33 -06:00
Maple Redleaf
11d9cedb27 Add individual snow code (not implemented) 2025-12-11 13:53:53 -06:00
13 changed files with 303 additions and 48 deletions

View File

@@ -7,8 +7,9 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Dependencies # Dependencies
set(RAYLIB_VERSION 5.5) set(RAYLIB_VERSION 5.5)
find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED
if(NOT raylib_FOUND) # If there's none, fetch and build raylib
include(FetchContent) include(FetchContent)
set(FETCHCONTENT_QUIET NO)
if(NOT raylib_DIR) # If there's none, fetch and build raylib
FetchContent_Declare( FetchContent_Declare(
raylib raylib
DOWNLOAD_EXTRACT_TIMESTAMP OFF DOWNLOAD_EXTRACT_TIMESTAMP OFF
@@ -16,26 +17,16 @@ if(NOT raylib_FOUND) # If there's none, fetch and build raylib
) )
FetchContent_GetProperties(raylib) FetchContent_GetProperties(raylib)
if(NOT raylib_POPULATED) # Have we downloaded raylib yet? if(NOT raylib_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_MakeAvailable(raylib) FetchContent_MakeAvailable(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( FetchContent_Declare(
raylib_cpp raylib_cpp
DOWNLOAD_EXTRACT_TIMESTAMP OFF GIT_REPOSITORY https://github.com/RobLoach/raylib-cpp.git
URL https://github.com/RobLoach/raylib-cpp/archive/refs/tags/${RAYLIB_CPP_VERSION}.tar.gz GIT_TAG master
) )
FetchContent_GetProperties(raylib_cpp)
if(NOT raylib_cpp_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_MakeAvailable(raylib_cpp) FetchContent_MakeAvailable(raylib_cpp)
endif()
endif()
# Our Project # Our Project
add_executable(${PROJECT_NAME}) add_executable(${PROJECT_NAME})

Binary file not shown.

Before

Width:  |  Height:  |  Size: 102 B

BIN
assets/tree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

31
src/consts.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include "Color.hpp"
#include <array>
#include <raylib.h>
#include <sys/types.h>
// SCREEN CONSTS
const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 60;
const int WIN_WIDTH = 1280;
const int WIN_HEIGHT = 720;
namespace Snow {
const int MAX_CYCLE_FRAMES = 4;
const int INV_HORMOVE_CHANCE = 5;
} // namespace Snow
namespace Lights {
const int MAX_CYCLE_FRAMES = 30;
const std::array<Vector2, 9> POSITIONS{{{28, 17},
{36, 15},
{49, 16},
{22, 31},
{34, 31},
{61, 30},
{59, 42},
{28, 28},
{47, 29}}};
const raylib::Color col1 = raylib::Color::Green();
const raylib::Color col2 = raylib::Color::Red();
} // namespace Lights

38
src/lights/light.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include "light.h"
#include "../consts.cpp"
#include "Vector2.hpp"
#include "raylib.h"
#include <cstdio>
namespace Lights {
Light::Light(raylib::Vector2 position, raylib::Color color1,
raylib::Color color2, int radius, LightState startingState,
int startingCycle)
: position(position), radius(radius), color1(color1), color2(color2),
state(startingState), cycle(startingCycle) {};
Light::~Light() = default;
void Light::Update() {
cycle++;
if (cycle >= MAX_CYCLE_FRAMES) {
SwitchState();
cycle = 0;
}
}
void Light::Draw() {
raylib::Color col = (state == Lights::LightState::color1) ? color1 : color2;
DrawCircleV(position, radius + 1, col.Alpha(0.5));
DrawCircleV(position, radius, col);
}
Lights::LightState Light::SwitchState() {
switch (state) {
case Lights::LightState::color1:
return state = Lights::LightState::color2;
case Lights::LightState::color2:
return state = Lights::LightState::color1;
}
return state;
}
} // namespace Lights

29
src/lights/light.h Normal file
View File

@@ -0,0 +1,29 @@
#include "Color.hpp"
#include "Vector2.hpp"
namespace Lights {
enum LightState {
color1,
color2,
};
class Light {
public:
Light(raylib::Vector2 position, raylib::Color color1, raylib::Color color2,
int radius, LightState startingState, int startingCycle);
void Update();
void Draw();
~Light();
private:
LightState SwitchState();
raylib::Vector2 position;
int radius;
raylib::Color color1;
raylib::Color color2;
LightState state;
int cycle;
};
} // namespace Lights

31
src/lights/manager.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include "manager.h"
#include "../consts.cpp"
#include <memory>
namespace Lights {
Manager::Manager() {
int cycle = 0;
lights.reserve(POSITIONS.size());
for (const auto &position : POSITIONS) {
lights.emplace_back(new Light(
position, col1, col2, 2,
(cycle % 2 == 0) ? LightState::color1 : LightState::color2, 0));
cycle++;
}
};
Manager::~Manager() = default;
void Manager::Update() {
for (auto lighti = 0; lighti < lights.size(); lighti++) {
auto &light = lights[lighti];
light->Update();
}
}
void Manager::Draw() {
for (auto lighti = 0; lighti < lights.size(); lighti++)
lights[lighti]->Draw();
}
} // namespace Lights

16
src/lights/manager.h Normal file
View File

@@ -0,0 +1,16 @@
#include "light.h"
#include <memory>
#include <vector>
namespace Lights {
class Manager {
public:
Manager();
void Update();
void Draw();
~Manager();
private:
std::vector<std::unique_ptr<Light>> lights;
};
} // namespace Lights

View File

@@ -1,17 +1,16 @@
#include "../data/cube.png.h" #include "../data/tree.png.h"
#include "raylib.h" #include "Functions.hpp"
#include <cmath> #include "Vector2.hpp"
#include "consts.cpp"
#include "lights/manager.h"
#include "snow/manager.h"
#include <raylib-cpp.hpp> #include <raylib-cpp.hpp>
#include <raylib.h>
#if defined(PLATFORM_WEB) #if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #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 MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -22,25 +21,20 @@ raylib::Window window(WIN_WIDTH, WIN_HEIGHT, "game",
raylib::RenderTexture2D target(SCREEN_WIDTH, SCREEN_HEIGHT); raylib::RenderTexture2D target(SCREEN_WIDTH, SCREEN_HEIGHT);
raylib::Image texImg = raylib::Image treeImg =
raylib::LoadImageFromMemory(".png", cube_png, cube_png_len); raylib::LoadImageFromMemory(".png", tree_png, tree_png_len);
raylib::Texture2D texture = texImg.LoadTexture(); raylib::Texture2D tree = treeImg.LoadTexture();
raylib::Camera3D cam(raylib::Vector3(0.0f, 0.0f, std::sqrt(3.0f)), Snow::Manager sMgr;
raylib::Vector3::Zero(), raylib::Vector3(0.0f, 1.0f, 0.0f), Lights::Manager lMgr;
60.0f, CAMERA_PERSPECTIVE);
raylib::Model cube(GenMeshCube(1.0f, 1.0f, 1.0f));
int main(void) { int main(void) {
window.SetMinSize({320, 240}); window.SetMinSize({SCREEN_WIDTH, SCREEN_HEIGHT});
window.SetTargetFPS(60); window.SetTargetFPS(60);
window.SetExitKey(KEY_BACKSPACE); window.SetExitKey(KEY_BACKSPACE);
target.GetTexture().SetFilter(TEXTURE_FILTER_POINT); tree.SetFilter(TEXTURE_FILTER_POINT);
cube.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = texture;
#if defined(PLATFORM_WEB) #if defined(PLATFORM_WEB)
emscripten_set_main_loop(MainLoop, 0, 1); emscripten_set_main_loop(MainLoop, 0, 1);
@@ -60,21 +54,15 @@ void MainLoop() {
float scale = float scale =
MIN((float)winWidth / SCREEN_WIDTH, (float)winHeight / SCREEN_HEIGHT); MIN((float)winWidth / SCREEN_WIDTH, (float)winHeight / SCREEN_HEIGHT);
cube.SetTransform( sMgr.Update();
raylib::Matrix::RotateXYZ( lMgr.Update();
raylib::Vector3(1.0f, 1.5f, 2.5f).Scale(window.GetFrameTime()))
.Multiply(cube.GetTransform()));
target.BeginMode(); target.BeginMode();
{ {
ClearBackground(BLACK); // ClearBackground(BLACK);
raylib::Vector2(160.0f, 120.0f) DrawTexture(tree, 0, 0, raylib::Color::White());
.DrawCircle(120.0f, raylib::Color::White()); lMgr.Draw();
cam.BeginMode(); sMgr.Draw();
{
cube.Draw(raylib::Vector3::Zero());
}
cam.EndMode();
} }
target.EndMode(); target.EndMode();

40
src/snow/manager.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include "manager.h"
#include <memory>
namespace Snow {
Manager::Manager() = default;
Manager::~Manager() = default;
void Manager::NewSnow() { snows.emplace_back(new Snowflake()); }
void Manager::NewSnows(int count) {
snows.reserve(snows.size() + count);
for (int i = 0; i < count; i++)
NewSnow();
}
void Manager::Update() {
frames++;
if (frames % 2 == 1)
NewSnow();
for (auto snowi = 0; snowi < snows.size(); snowi++) {
auto &snow = snows[snowi];
if (!snow->IsAlive()) {
Kill(snowi);
continue;
}
snow->Update();
}
}
void Manager::Draw() {
for (auto snowi = 0; snowi < snows.size(); snowi++)
snows[snowi]->Draw();
}
void Manager::Kill(int pos) {
std::swap(snows[pos], snows.back());
snows.pop_back();
}
} // namespace Snow

20
src/snow/manager.h Normal file
View File

@@ -0,0 +1,20 @@
#include "snow.h"
#include <memory>
#include <vector>
namespace Snow {
class Manager {
public:
Manager();
void NewSnows(int snows);
void NewSnow();
void Update();
void Draw();
~Manager();
private:
std::vector<std::unique_ptr<Snowflake>> snows;
void Kill(int pos);
int frames = 0;
};
} // namespace Snow

46
src/snow/snow.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "snow.h"
#include "../consts.cpp"
#include "Color.hpp"
#include "Vector2.hpp"
#include <raylib.h>
namespace Snow {
Snowflake::Snowflake() : Snowflake(GetRandomValue(0, 320)) {}
Snowflake::Snowflake(int x) : Snowflake(raylib::Vector2(x, 0)) {}
Snowflake::Snowflake(raylib::Vector2 pos) : position(pos) {}
void Snowflake::Move() {
raylib::Vector2 move;
int hMove = GetRandomValue(0, INV_HORMOVE_CHANCE);
if (hMove <= 1) {
hMove = hMove * 2 - 1;
} else {
hMove = 0;
}
move.x = hMove;
move.y = 1;
position += move;
}
bool Snowflake::IsAlive() { return live; }
void Snowflake::CheckLive() {
if (position.y >= SCREEN_HEIGHT) {
live = false;
}
}
void Snowflake::Update() {
if (currCycle >= MAX_CYCLE_FRAMES) {
Move();
CheckLive();
currCycle = 0;
return;
}
currCycle++;
}
void Snowflake::Draw() { raylib::Color::White().DrawPixel(position); }
Snowflake::~Snowflake() = default;
} // namespace Snow

25
src/snow/snow.h Normal file
View File

@@ -0,0 +1,25 @@
#include <raylib-cpp.hpp>
#include <sys/types.h>
namespace Snow {
class Snowflake {
public:
Snowflake();
Snowflake(int x);
Snowflake(raylib::Vector2 pos);
void Update();
void Draw();
bool IsAlive();
~Snowflake();
private:
raylib::Vector2 position;
int currCycle = 0;
bool live = true;
inline void Move();
inline void CheckLive();
};
} // namespace Snow