all repos — hyperkaos @ 8b1e4cafdf8b83c958d3b5c2dd5a9543a35f318b

lightweight modular puzzle/adventure game engine in C with SDL 1.2

added alpha blending for scene components, global pointer for scene data, include file for the intro scene, etc
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAlupL8UACgkQO3+8IhRO
Y5hptBAAmRmBzoi4vZzH98S0y01rNufQSk8TTnLDRmpdQcf3FD+1XaU9aTwlA4oN
NQhyr1Lcf932jG1RNqGZS/fncZUITdVj2SdZG9foMcvKJRKXQGZKnSgALpqRuUU0
872fXO2Wgd6SxSNxPNLUcFqD0nsu2Xj656jsohiNtMhkF4xkYF7iN8zCxTQlX01H
jHs4fLQZlpBFhsnjQ68KV+5GAA+bvOTiCVz42mVtwtNAevMOiVRrC5VzeqZ4FpEp
b1N9SmkdVTTQZMXt+CJccX6VHTsr35hdANoe6zpQFqtHdF0KieQgSdD9aVt1zLT0
hET2jP7bh709kIpBUjWbeH8QS+XtksfGYZxsh+CODHktULkAmsOnqEndhefRsgMr
EFWgUtSBunwcRvbcs2KCS9OQEj6Fmk3stsxkxLvKx9KZhbfOnL056xNE5v7XIEUJ
fcYmgu1XRTyn1n04HM+5qt9Nda8jGuN3xvMoGnxitgLAo3mkUPjQ1A/NX+dWPOaB
yphrMEHxR2l5QjsiMvAtK8FQc0r1CcxLcOPM18CpEEhKqpXeJbJVnjYX2HdKbDzf
y/38KrjhZeocUzzSHhYocGwaBpQqEjHSbQacjfyplDQQKqPA91np3Oy6xuKlfWiv
oco9rWFhoMUayDZAT7uGfVolAXcKd/oLlxzCWhpuvMDKI61tLMM=
=LuyQ
-----END PGP SIGNATURE-----
commit

8b1e4cafdf8b83c958d3b5c2dd5a9543a35f318b

parent

eb1d90d5e03dd01249922e8591b1c34d0599ffe4

A .gitignore

@@ -0,0 +1,5 @@

+# ignore compiled objects +/*.o + +# ignore the test executable +/game
M Engine.cEngine.c

@@ -15,6 +15,7 @@ #include "WorldData.h"

#include "TextBox.h" #include "Kaos.h" #include "HyperKaos.h" +#include "Scene.h" #include "extern.h" //

@@ -207,12 +208,13 @@ loadingTxt = TTF_RenderText_Solid(font, "loading map data...", textColor);

hero = newPlayer("assets/img/characters/kmage.png", 160, 90); - mapData = (Room**)malloc(64*sizeof(Room*)); - mapBuffer= (Room**)malloc(64*sizeof(Room*)); - dialogueData = (TextBox**)malloc(124*sizeof(Room*)); - bgmData = (Mix_Music**)malloc(4*sizeof(Mix_Music*)); - sfxData = (Mix_Chunk**)malloc(24*sizeof(Mix_Chunk*)); - kaosData = (Kaos**)malloc(124*sizeof(Kaos*)); + mapData = malloc(64*sizeof(Room*)); + mapBuffer= malloc(64*sizeof(Room*)); + dialogueData = malloc(124*sizeof(Room*)); + bgmData = malloc(8*sizeof(Mix_Music*)); + sfxData = malloc(24*sizeof(Mix_Chunk*)); + kaosData = malloc(124*sizeof(Kaos*)); + theatre = malloc(8*sizeof(Scene*)); printf("Init complete\n"); return 1; }

@@ -260,6 +262,8 @@ free(dialogueData);

free(bgmData); free(sfxData); + + free(theatre); SDL_FreeSurface(textBoxBG); SDL_FreeSurface(nextArrow);
M HyperKaos.cHyperKaos.c

@@ -11,6 +11,7 @@ typedef struct room Room;

typedef struct player Player; typedef struct timer Timer; typedef struct textBox TextBox; +typedef struct scene Scene; #include "extern.h"
M Kaos.cKaos.c

@@ -11,6 +11,7 @@ #include "TextBox.h"

#include "Player.h" #include "Room.h" #include "HyperKaos.h" +#include "Scene.h" #include "extern.h"
M MakefileMakefile

@@ -4,7 +4,10 @@ .PHONY: all clean cleanobj

all: game -game: main.c Player.o Engine.o Timer.o Room.o WorldData.o TextBox.o Kaos.o HyperKaos.o +scenetest: sceneTest.c Scene.o Engine.o Timer.o Player.o Room.o WorldData.o Kaos.o HyperKaos.o TextBox.o + $(CC) -o $@ $^ $(CFLAGS) + +game: main.c Player.o Engine.o Timer.o Room.o WorldData.o TextBox.o Kaos.o HyperKaos.o Scene.o $(CC) -o $@ $^ $(CFLAGS) cleanobj:
M Player.cPlayer.c

@@ -14,6 +14,7 @@ typedef struct TTF_Font TTF_Font;

typedef struct timer Timer; typedef struct textBox TextBox; typedef struct kaos Kaos; +typedef struct scene Scene; #include "extern.h" Player* newPlayer(char* filename, int a, int b)
M Room.cRoom.c

@@ -13,6 +13,7 @@ #include "HyperKaos.h"

typedef struct timer Timer; typedef struct textBox TextBox; +typedef struct scene Scene; #include "extern.h" Room* newRoom(char* filename, int a)
M Scene.cScene.c

@@ -15,11 +15,11 @@ typedef struct room Room;

typedef struct player Player; #include "extern.h" -SLayer* newSLayer(char* filename, int x, int y, int h, int v) +SLayer* newSLayer(SDL_Surface* img, int x, int y, int h, int v, int alpha) { SLayer* self = malloc(sizeof(SLayer)); - self->sprite = loadImage(filename); + self->sprite = img; self->x = x; self->y = y; self->h = h;

@@ -27,6 +27,9 @@ self->v = v;

self->oX = x; self->oY = y; + + if (alpha != 255) + SDL_SetAlpha(self->sprite, SDL_SRCALPHA|SDL_RLEACCEL, alpha); return self; }

@@ -55,13 +58,14 @@ int i;

SLayer** temp; if (self->nSprites) temp = self->sprites; - self->sprites = malloc((self->nSprites+1)*sizeof(SLayer)); + self->sprites = malloc((self->nSprites+1)*sizeof(SLayer*)); if (self->nSprites) { for (i = 0; i < self->nSprites; i++) self->sprites[i] = temp[i]; } - self->sprites[self->nSprites++] = sprite; + self->sprites[self->nSprites] = sprite; + self->nSprites++; } void playScene(Scene* self)
M Scene.hScene.h

@@ -21,7 +21,7 @@ int time;

Transition fade; } Scene; -SLayer* newSLayer(char* filename, int x, int y, int h, int v); +SLayer* newSLayer(SDL_Surface* img, int x, int y, int h, int v, int alpha); void deleteSLayer(SLayer* target); Scene* newScene(int in, int out, int time, SDL_Color incolor, SDL_Color outcolor);
M TextBox.cTextBox.c

@@ -7,22 +7,11 @@ #include "Timer.h"

#include "Player.h" #include "TextBox.h" -extern int quit; -extern int playing; -extern int actionbutton; +typedef struct room Room; +typedef struct kaos Kaos; +typedef struct scene Scene; -extern SDL_Event event; -extern SDL_Surface* screen; -extern Timer fps; - -extern SDL_Rect* textScroller; -extern SDL_Surface* textBoxBG; -extern SDL_Surface* nextArrow; - -extern TTF_Font* font; -extern SDL_Color textColor; - -extern Player* hero; +#include "extern.h" TextBox* newTextBox() {
M WorldData.cWorldData.c

@@ -11,6 +11,7 @@ #include "Room.h"

#include "Kaos.h" #include "HyperKaos.h" #include "TextBox.h" +#include "Scene.h" typedef struct timer Timer; #include "extern.h"
M enum.henum.h

@@ -1,5 +1,3 @@

-#define CHUNKS_H - enum dataChunks { LEVEL1 = 1,
M extern.hextern.h

@@ -38,4 +38,4 @@ extern TextBox** dialogueData;

extern Mix_Music** bgmData; extern Mix_Chunk** sfxData; extern Kaos** kaosData; - +extern Scene** theatre;
A intro.c

@@ -0,0 +1,25 @@

+ + SDL_Color black = {0,0,0}; + + SLayer* nebula = newSLayer(loadImage("assets/img/backgrounds/presents.png"), 0,0,0,0,255); + SLayer* fogF = newSLayer(loadImage("assets/img/fx/fog.png"), 0,0,-1,0,128); + SLayer* fogB = newSLayer(loadImage("assets/img/fx/plasma.png"), -320,0,1,0,56); + SLayer* presents = newSLayer(TTF_RenderText_Solid(font, "nilFM presents", textColor), 120,84,0,0,128); + + SLayer* menuTransition = newSLayer(loadImage("assets/img/backgrounds/mainmenu.png"),0,0,0,0,255); + + Scene* intro = newScene(30,30, 200, black, black); + Scene* transition = newScene(30,0,30, black, black); + + buildScene(intro, nebula); + buildScene(intro, fogB); + buildScene(intro, fogF); + buildScene(intro, presents); + + buildScene(transition, menuTransition); + + playScene(intro); + playScene(transition); + + deleteScene(intro); + deleteScene(transition);
M main.cmain.c

@@ -55,6 +55,7 @@ TextBox** dialogueData = NULL;

Mix_Music** bgmData = NULL; Mix_Chunk** sfxData = NULL; Kaos** kaosData = NULL; +Scene** theatre = NULL; int kaosFlag = -1;

@@ -69,17 +70,12 @@ printf("Init failed\n");

return 1; } - SDL_Color black = {0,0,0}; - SLayer* presents = newSLayer("assets/img/backgrounds/presents.png", 0,0,0,0); - SLayer* menuTransition = newSLayer("assets/img/backgrounds/mainmenu.png",0,0,0,0); - Scene* intro = newScene(30,30, 90, black, black); - Scene* transition = newScene(30,0,30, black, black); - buildScene(intro, presents); - buildScene(transition, menuTransition); - playScene(intro); - playScene(transition); - deleteScene(intro); - deleteScene(transition); + /* + * intro discarded immediately after playing, so instead of increasing the + * complexity and offloading it somewhere, we just keep it in this include + * file for cleanliness and modularity + */ + #include "intro.c" // main game loop while (!quit)