-
Notifications
You must be signed in to change notification settings - Fork 12
/
raylib_game.c
143 lines (115 loc) · 5.53 KB
/
raylib_game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************************
*
* raylib gamejam template
*
* Template originally created with raylib 4.5-dev, last time updated with raylib 5.0
*
* Template licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2022-2024 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#if defined(PLATFORM_WEB)
#define CUSTOM_MODAL_DIALOGS // Force custom modal dialogs usage
#include <emscripten/emscripten.h> // Emscripten library - LLVM to JavaScript compiler
#endif
#include <stdio.h> // Required for: printf()
#include <stdlib.h> // Required for:
#include <string.h> // Required for:
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
// Simple log system to avoid printf() calls if required
// NOTE: Avoiding those calls, also avoids const strings memory usage
#define SUPPORT_LOG_INFO
#if defined(SUPPORT_LOG_INFO)
#define LOG(...) printf(__VA_ARGS__)
#else
#define LOG(...)
#endif
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef enum {
SCREEN_LOGO = 0,
SCREEN_TITLE,
SCREEN_GAMEPLAY,
SCREEN_ENDING
} GameScreen;
// TODO: Define your custom data types here
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static const int screenWidth = 1280;
static const int screenHeight = 720;
static RenderTexture2D target = { 0 }; // Render texture to render our game
// TODO: Define global variables here, recommended to make them static
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
static void UpdateDrawFrame(void); // Update and Draw one frame
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
#if !defined(_DEBUG)
SetTraceLogLevel(LOG_NONE); // Disable raylib trace log messages
#endif
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib gamejam template");
// TODO: Load resources / Initialize variables at this point
// Render texture to draw full screen, enables screen scaling
// NOTE: If screen is scaled, mouse input should be scaled proportionally
target = LoadRenderTexture(screenWidth, screenHeight);
SetTextureFilter(target.texture, TEXTURE_FILTER_BILINEAR);
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 60, 1);
#else
SetTargetFPS(60); // Set our game frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button
{
UpdateDrawFrame();
}
#endif
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadRenderTexture(target);
// TODO: Unload all loaded resources at this point
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//--------------------------------------------------------------------------------------------
// Module functions definition
//--------------------------------------------------------------------------------------------
// Update and draw frame
void UpdateDrawFrame(void)
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update variables / Implement example logic at this point
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
// Render game screen to a texture,
// it could be useful for scaling or further shader postprocessing
BeginTextureMode(target);
ClearBackground(RAYWHITE);
// TODO: Draw your game screen here
DrawRectangle(10, 10, screenWidth - 20, screenHeight - 20, SKYBLUE);
EndTextureMode();
// Render to screen (main framebuffer)
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw render texture to screen, scaled if required
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);
// TODO: Draw everything that requires to be drawn at this point, maybe UI?
EndDrawing();
//----------------------------------------------------------------------------------
}