Skip to content

Commit 73403a0

Browse files
committed
head movement
1 parent a719eb1 commit 73403a0

10 files changed

Lines changed: 337 additions & 55 deletions

File tree

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ PLATFORM ?= PLATFORM_DESKTOP
3232
PROJECT_NAME ?= raylib_game
3333
PROJECT_VERSION ?= 1.0
3434
PROJECT_BUILD_PATH ?= .
35-
PROJECT_SOURCE_FILES ?= raylib_game.c
35+
PROJECT_SOURCE_FILES ?= raylib_game.c clock.c input.c snake.c
3636

3737
# raylib library variables
3838
RAYLIB_SRC_PATH ?= C:/raylib/raylib/src

src/clock.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "clock.h"
2+
3+
Clock InitClock(float start)
4+
{
5+
return (Clock){
6+
.rate = start,
7+
.age = 0.f,
8+
};
9+
}
10+
11+
bool TickClock(Clock *clock)
12+
{
13+
clock->age += GetFrameTime();
14+
15+
if (clock->age >= clock->rate)
16+
{
17+
clock->age -= clock->rate;
18+
return true;
19+
}
20+
else
21+
{
22+
return false;
23+
}
24+
}
25+
26+
void DecreaseClockRate(Clock *clock)
27+
{
28+
clock->rate *= 0.9f;
29+
}
30+
31+
void SetClockRate(Clock *clock, float rate)
32+
{
33+
clock->rate = rate;
34+
}

src/clock.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef CLOCK_H
2+
#define CLOCK_H
3+
4+
#include "raylib.h"
5+
6+
typedef struct Clock
7+
{
8+
float rate;
9+
float age;
10+
} Clock;
11+
12+
Clock InitClock(float start);
13+
bool TickClock(Clock *clock);
14+
void DecreaseClockRate(Clock *clock);
15+
void SetClockRate(Clock *clock, float rate);
16+
17+
#endif

src/globals.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef GLOBALS_H
2+
#define GLOBALS_H
3+
4+
#define LEVEL_SIZE 16
5+
#define MAX_CELLS 128
6+
7+
#define CELL_SIZE 1.f
8+
9+
// typedef struct Axise
10+
// {
11+
// Vector3 forward;
12+
// Vector3 left;
13+
// Vector3 up;
14+
// } Axise;
15+
16+
#endif

src/input.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "input.h"
2+
3+
Direction HandleInput()
4+
{
5+
if (IsKeyDown(KEY_UP))
6+
return DIRECTION_UP;
7+
if (IsKeyDown(KEY_LEFT))
8+
return DIRECTION_LEFT;
9+
if (IsKeyDown(KEY_DOWN))
10+
return DIRECTION_DOWN;
11+
if (IsKeyDown(KEY_RIGHT))
12+
return DIRECTION_RIGHT;
13+
else
14+
return DIRECTION_NONE;
15+
}

src/input.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef INPUT_H
2+
#define INPUT_H
3+
4+
#include "raylib.h"
5+
6+
typedef enum Direction
7+
{
8+
DIRECTION_NONE,
9+
DIRECTION_UP,
10+
DIRECTION_LEFT,
11+
DIRECTION_DOWN,
12+
DIRECTION_RIGHT,
13+
} Direction;
14+
15+
Direction HandleInput(void);
16+
17+
#endif

src/raylib_game

14.1 KB
Binary file not shown.

src/raylib_game.c

Lines changed: 91 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
/*******************************************************************************************
2-
*
3-
* raylib gamejam template
4-
*
5-
* Template originally created with raylib 4.5-dev, last time updated with raylib 5.0
6-
*
7-
* Template licensed under an unmodified zlib/libpng license, which is an OSI-certified,
8-
* BSD-like license that allows static linking with closed source software
9-
*
10-
* Copyright (c) 2022-2025 Ramon Santamaria (@raysan5)
11-
*
12-
********************************************************************************************/
2+
*
3+
* raylib gamejam template
4+
*
5+
* Template originally created with raylib 4.5-dev, last time updated with raylib 5.0
6+
*
7+
* Template licensed under an unmodified zlib/libpng license, which is an OSI-certified,
8+
* BSD-like license that allows static linking with closed source software
9+
*
10+
* Copyright (c) 2022-2025 Ramon Santamaria (@raysan5)
11+
*
12+
********************************************************************************************/
1313

1414
#include "raylib.h"
15+
#include "raymath.h"
1516

1617
#if defined(PLATFORM_WEB)
17-
#define CUSTOM_MODAL_DIALOGS // Force custom modal dialogs usage
18-
#include <emscripten/emscripten.h> // Emscripten library - LLVM to JavaScript compiler
18+
#define CUSTOM_MODAL_DIALOGS // Force custom modal dialogs usage
19+
#include <emscripten/emscripten.h> // Emscripten library - LLVM to JavaScript compiler
1920
#endif
2021

21-
#include <stdio.h> // Required for: printf()
22-
#include <stdlib.h> // Required for:
23-
#include <string.h> // Required for:
22+
#include <stdio.h> // Required for: printf()
23+
#include <stdlib.h> // Required for:
24+
#include <string.h> // Required for:
2425

2526
//----------------------------------------------------------------------------------
2627
// Defines and Macros
@@ -29,18 +30,23 @@
2930
// NOTE: Avoiding those calls, also avoids const strings memory usage
3031
#define SUPPORT_LOG_INFO
3132
#if defined(SUPPORT_LOG_INFO)
32-
#define LOG(...) printf(__VA_ARGS__)
33+
#define LOG(...) printf(__VA_ARGS__)
3334
#else
34-
#define LOG(...)
35+
#define LOG(...)
3536
#endif
3637

38+
#include "globals.h"
39+
#include "clock.h"
40+
#include "snake.h"
41+
3742
//----------------------------------------------------------------------------------
3843
// Types and Structures Definition
3944
//----------------------------------------------------------------------------------
40-
typedef enum {
41-
SCREEN_LOGO = 0,
42-
SCREEN_TITLE,
43-
SCREEN_GAMEPLAY,
45+
typedef enum
46+
{
47+
SCREEN_LOGO = 0,
48+
SCREEN_TITLE,
49+
SCREEN_GAMEPLAY,
4450
SCREEN_ENDING
4551
} GameScreen;
4652

@@ -49,33 +55,52 @@ typedef enum {
4955
//----------------------------------------------------------------------------------
5056
// Global Variables Definition
5157
//----------------------------------------------------------------------------------
52-
static const int screenWidth = 800;
53-
static const int screenHeight = 450;
58+
static const int screenWidth = 1200;
59+
static const int screenHeight = 900;
60+
61+
static RenderTexture2D target = {0}; // Render texture to render our game
5462

55-
static RenderTexture2D target = { 0 }; // Render texture to render our game
63+
static const Vector3 cameraNeck = (Vector3){0.f, 4.f, 6.f};
5664

5765
// TODO: Define global variables here, recommended to make them static
5866

5967
//----------------------------------------------------------------------------------
6068
// Module Functions Declaration
6169
//----------------------------------------------------------------------------------
62-
static void UpdateDrawFrame(void); // Update and Draw one frame
70+
static void UpdateDrawFrame(Camera3D camera, Clock *clock, Snake *snake); // Update and Draw one frame
6371

6472
//------------------------------------------------------------------------------------
6573
// Program main entry point
6674
//------------------------------------------------------------------------------------
6775
int main(void)
6876
{
6977
#if !defined(_DEBUG)
70-
SetTraceLogLevel(LOG_NONE); // Disable raylib trace log messages
78+
SetTraceLogLevel(LOG_NONE); // Disable raylib trace log messages
7179
#endif
7280

7381
// Initialization
7482
//--------------------------------------------------------------------------------------
7583
InitWindow(screenWidth, screenHeight, "raylib gamejam template");
76-
84+
SetWindowMonitor(0);
85+
7786
// TODO: Load resources / Initialize variables at this point
78-
87+
88+
// Define the camera to look into our 3d world
89+
Vector3 start = (Vector3){0.f, 0.f, 0.f};
90+
Vector3 forward = (Vector3){0.f, 0.f, -1.f};
91+
92+
Camera3D camera = {0};
93+
camera.position = cameraNeck; // Camera position
94+
camera.target = start; // Camera looking at point
95+
camera.up = (Vector3){0.0f, 1.0f, 0.0f}; // Camera up vector (rotation towards target)
96+
camera.fovy = 85.0f; // Camera field-of-view Y
97+
camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
98+
99+
Snake snake = InitSnake(&start, forward);
100+
101+
// movement clock
102+
Clock clock = InitClock(1.f);
103+
79104
// Render texture to draw full screen, enables screen scaling
80105
// NOTE: If screen is scaled, mouse input should be scaled proportionally
81106
target = LoadRenderTexture(screenWidth, screenHeight);
@@ -84,23 +109,23 @@ int main(void)
84109
#if defined(PLATFORM_WEB)
85110
emscripten_set_main_loop(UpdateDrawFrame, 60, 1);
86111
#else
87-
SetTargetFPS(60); // Set our game frames-per-second
112+
SetTargetFPS(60); // Set our game frames-per-second
88113
//--------------------------------------------------------------------------------------
89114

90115
// Main game loop
91-
while (!WindowShouldClose()) // Detect window close button
116+
while (!WindowShouldClose()) // Detect window close button
92117
{
93-
UpdateDrawFrame();
118+
UpdateDrawFrame(camera, &clock, &snake);
94119
}
95120
#endif
96121

97122
// De-Initialization
98123
//--------------------------------------------------------------------------------------
99124
UnloadRenderTexture(target);
100-
125+
101126
// TODO: Unload all loaded resources at this point
102127

103-
CloseWindow(); // Close window and OpenGL context
128+
CloseWindow(); // Close window and OpenGL context
104129
//--------------------------------------------------------------------------------------
105130

106131
return 0;
@@ -110,35 +135,47 @@ int main(void)
110135
// Module functions definition
111136
//--------------------------------------------------------------------------------------------
112137
// Update and draw frame
113-
void UpdateDrawFrame(void)
138+
void UpdateDrawFrame(Camera3D camera, Clock *clock, Snake *snake)
114139
{
115140
// Update
116141
//----------------------------------------------------------------------------------
117-
// TODO: Update variables / Implement example logic at this point
118-
//----------------------------------------------------------------------------------
142+
if (TickClock(clock))
143+
{
144+
printf("\ntick - ");
145+
MoveSnake(snake);
146+
147+
// printf("\tupdate camera - ");
148+
camera.position = Vector3Add(*snake->body[0], cameraNeck);
149+
camera.target = Vector3Add(camera.position, snake->forward);
150+
camera.up = snake->up;
151+
UpdateCamera(&camera, CAMERA_THIRD_PERSON);
152+
}
119153

120154
// Draw
121155
//----------------------------------------------------------------------------------
122-
// Render game screen to a texture,
156+
// Render game screen to a texture,
123157
// it could be useful for scaling or further shader postprocessing
124-
BeginTextureMode(target);
125-
ClearBackground(RAYWHITE);
126-
127-
// TODO: Draw your game screen here
128-
DrawText("Welcome to raylib NEXT gamejam!", 150, 140, 30, BLACK);
129-
DrawRectangleLinesEx((Rectangle){ 0, 0, screenWidth, screenHeight }, 16, BLACK);
130-
131-
EndTextureMode();
132-
158+
// BeginTextureMode(target);
159+
// ClearBackground(RAYWHITE);
160+
161+
// // TODO: Draw your game screen here
162+
// DrawText("Welcome to raylib NEXT gamejam!", 150, 140, 30, BLACK);
163+
// DrawRectangleLinesEx((Rectangle){0, 0, screenWidth, screenHeight}, 16, BLACK);
164+
165+
// EndTextureMode();
166+
133167
// Render to screen (main framebuffer)
134168
BeginDrawing();
135-
ClearBackground(RAYWHITE);
136-
137-
// Draw render texture to screen, scaled if required
138-
DrawTexturePro(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, -(float)target.texture.height }, (Rectangle){ 0, 0, (float)target.texture.width, (float)target.texture.height }, (Vector2){ 0, 0 }, 0.0f, WHITE);
169+
ClearBackground(RAYWHITE);
170+
BeginMode3D(camera);
171+
172+
// Draw render texture to screen, scaled if required
173+
DrawSnake(snake);
174+
DrawGrid(16, 1.0f);
139175

140-
// TODO: Draw everything that requires to be drawn at this point, maybe UI?
176+
EndMode3D();
177+
// TODO: Draw everything that requires to be drawn at this point, maybe UI?
141178

142179
EndDrawing();
143-
//----------------------------------------------------------------------------------
144-
}
180+
//----------------------------------------------------------------------------------
181+
}

0 commit comments

Comments
 (0)