summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c18
-rw-r--r--src/scenes/mainMenu.c24
-rw-r--r--src/scenes/sceneManager.c47
-rw-r--r--src/scenes/scenes.h31
4 files changed, 95 insertions, 25 deletions
diff --git a/src/main.c b/src/main.c
index 9679dab..af955f0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,18 +2,26 @@
#include <raylib.h>
#include "scenes/scenes.h"
-enum SCENE current_scene = MAIN_MENU;
-
int main(void) {
- printf("Hello, world!\n");
+ if (!InitSceneStack()) {
+ printf("Failed to create a scene stack\n");
+ return 1;
+ }
+
+ if (!AddScene(CreateMainMenu())) {
+ printf("Failed to create a main menu\n");
+ return 1;
+ }
InitWindow(800, 600, "Programming game");
SetTargetFPS(60);
while (!WindowShouldClose()) {
BeginDrawing();
- UpdateScene(current_scene);
- DrawScene(current_scene);
+
+ UpdateScene();
+ DrawScene();
+
EndDrawing();
}
diff --git a/src/scenes/mainMenu.c b/src/scenes/mainMenu.c
index 07ba34d..31b52c0 100644
--- a/src/scenes/mainMenu.c
+++ b/src/scenes/mainMenu.c
@@ -1,9 +1,27 @@
#include <raylib.h>
+#include <stdlib.h>
+#include "scenes.h"
-void MainMenuUpdate() {
+bool updateMainMenu(void *self) {
+ return true;
}
-void MainMenuDraw() {
+bool drawMainMenu(void *self) {
ClearBackground(SKYBLUE);
- DrawText("MAIN MENU", 10, 10, 30, BLACK);
+ DrawText("Main menu", 0, 0, 100, BLACK);
+ return true;
+}
+
+void deinitMainMenu(void *self) {
+ TraceLog(LOG_INFO, "Deleting a main menu");
+ free((Scene *)self);
+}
+
+Scene *CreateMainMenu() {
+ TraceLog(LOG_INFO, "Creating a main menu");
+ Scene *outp = malloc(sizeof(Scene));
+ outp->Update = updateMainMenu;
+ outp->Draw = drawMainMenu;
+ outp->Deint = deinitMainMenu;
+ return outp;
}
diff --git a/src/scenes/sceneManager.c b/src/scenes/sceneManager.c
index 4d42ed9..bb70864 100644
--- a/src/scenes/sceneManager.c
+++ b/src/scenes/sceneManager.c
@@ -1,14 +1,45 @@
#include "scenes.h"
+#include <stdlib.h>
+#include <sys/types.h>
-void UpdateScene(enum SCENE scene) {
- switch (scene) {
- case MAIN_MENU: MainMenuUpdate();
- }
+Scene **sceneStack = NULL;
+uint sceneStackDepth = 0;
+
+Scene *getCurrentScene() {
+ if (sceneStackDepth == 0) return false;
+ return sceneStack[sceneStackDepth - 1];
+}
+
+bool InitSceneStack() {
+ sceneStack = malloc(sizeof(Scene) * SCENE_STACK_SIZE);
+ return sceneStack != NULL;
+}
+
+bool AddScene(Scene *newScene) {
+ if (newScene == NULL || sceneStackDepth == SCENE_STACK_SIZE)
+ return false;
+
+ sceneStack[sceneStackDepth] = newScene;
+ sceneStackDepth += 1;
+
+ return true;
}
-void DrawScene(enum SCENE scene) {
- switch (scene) {
- case MAIN_MENU: MainMenuDraw();
- }
+void DeleteSceneFromStack() {
+ Scene *currentScene = getCurrentScene();
+ if (currentScene == NULL) return;
+ currentScene->Deint(currentScene);
+ sceneStack[--sceneStackDepth] = NULL;
}
+bool DrawScene() {
+ Scene *currentScene = getCurrentScene();
+ if (currentScene == NULL) return false;
+ return currentScene->Draw(currentScene);
+}
+
+bool UpdateScene() {
+ Scene *currentScene = getCurrentScene();
+ if (currentScene == NULL) return false;
+ return currentScene->Update(currentScene);
+}
diff --git a/src/scenes/scenes.h b/src/scenes/scenes.h
index 88295e6..7ad34af 100644
--- a/src/scenes/scenes.h
+++ b/src/scenes/scenes.h
@@ -1,17 +1,30 @@
+#include <stdbool.h>
+
#ifndef SCENES_H
#define SCENES_H
-/// The scene that the action should be executed upon
-enum SCENE {
- MAIN_MENU,
-};
+#define SCENE_STACK_SIZE 16
+
+/// This structure represents a scene that is stored in a stack.
+/// The main appeal of this data type is dynamic creation of different
+/// scenes that don't depend on global state
+typedef struct {
+ bool (*Update)(void *self);
+ bool (*Draw)(void *self);
+ void (*Deint)(void *self);
+ /// A generic pointer to whatever data the scene needs. You will have to
+ /// cast it yourself
+ void *data;
+} Scene;
+
+bool InitSceneStack();
-void DrawScene(enum SCENE scene);
-void UpdateScene(enum SCENE scene);
+bool AddScene(Scene *newScene);
+void DeleteSceneFromStack();
+bool DrawScene();
+bool UpdateScene();
-// ========== MAIN MENU ==========
-void MainMenuUpdate();
-void MainMenuDraw();
+Scene *CreateMainMenu();
#endif // SCENES_H