Skip to content

Commit

Permalink
Merge pull request #7 from ErFer7/v2.16
Browse files Browse the repository at this point in the history
v2.16
  • Loading branch information
ErFer7 authored Aug 13, 2022
2 parents b1ef80b + 5e9347a commit 6991b9e
Show file tree
Hide file tree
Showing 14 changed files with 540 additions and 493 deletions.
95 changes: 4 additions & 91 deletions Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,104 +11,17 @@
* \______/ \______/ \_______/ \__| \__|\________| \_/ \______| \_/ \__| \__|
*/

#include "include/core.h"
#include "include/utilities.h"
#include "include/graphics.h"
#include "include/interface.h"
#include "include/entity.h"
#include "include/world.h"
#include "include/statemachine.h"

#define TICK 120
#define CONSOLE_WIDTH 120
#define CONSOLE_HEIGHT 30
#define WORLD_WIDTH 512
#define WORLD_HEIGHT 512

int main()
{
InitCore();
SetTick(TICK);
InitConsoleRenderer(CONSOLE_WIDTH, CONSOLE_HEIGHT);
InitInterface();

while (state != EXIT)
{
UpdateInterfaces();

LockEvent();
switch (GetGameEvent())
{
case UI_PLAY:

state = GAMEPLAY;
GenerateWorld(WORLD_WIDTH, WORLD_HEIGHT);
InitEntitySemaphores();
StartBehaviourThread();
StartPhysicsThread();
StartRenderingThread();
break;
case UI_INFO:

state = INFO_MENU;
break;
case UI_QUIT:

state = EXIT;
StopBehaviourThread();
StopPhysicsThread();
StopRenderingThread();
FreeEntityMatrix();
FreeEntitySemaphores();
break;
case UI_PAUSE:

state = PAUSE;
StopBehaviourThread();
StopPhysicsThread();
StopRenderingThread();
break;
case UI_RESUME:

state = GAMEPLAY;
InitEntitySemaphores();
StartBehaviourThread();
StartPhysicsThread();
StartRenderingThread();
break;
case UI_RESTART:

state = GAMEPLAY;
FreeEntityMatrix();
GenerateWorld(WORLD_WIDTH, WORLD_HEIGHT);
InitEntitySemaphores();
StartBehaviourThread();
StartPhysicsThread();
StartRenderingThread();
break;
case UI_RETURN:

state = MAIN_MENU;
FreeEntityMatrix();
break;
case GM_GAMEOVER:

state = GAMEOVER;
StopBehaviourThread();
StopPhysicsThread();
StopRenderingThread();
FreeEntityMatrix();
FreeEntitySemaphores();
break;
default:
break;
}

SetGameEvent(IDLE, 1);
UnlockEvent();
}

FreeCore();
FreeConsoleRenderer();
InitEventStateMachine(TICK, CONSOLE_WIDTH, CONSOLE_HEIGHT);
RunEventStateMachine();
FreeEventStateMachine();

return 0;
}
10 changes: 8 additions & 2 deletions include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
enum Event
{
IDLE,
UI_PLAY,
UI_START,
UI_START_SMALL,
UI_START_REGULAR,
UI_START_LARGE,
UI_START_MEGA,
UI_START_CLASSIC,
UI_INFO,
UI_QUIT,
UI_PAUSE,
Expand All @@ -19,6 +24,7 @@ enum State
{
MAIN_MENU,
INFO_MENU,
START_MENU,
GAMEPLAY,
PAUSE,
GAMEOVER,
Expand All @@ -32,7 +38,7 @@ extern pthread_mutex_t eventMutex;

void InitCore();
void FreeCore();
void SetGameEvent(enum Event newEvent, int force);
void SetGameEvent(enum Event event_, int force);
enum Event GetGameEvent();
void LockEvent();
void UnlockEvent();
3 changes: 2 additions & 1 deletion include/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extern pthread_t physicsThread;
extern pthread_t renderingThread;
extern sem_t behaviourSemaphore;
extern sem_t physicsSemaphore;
extern int fixedScreen;

void InitEntitySemaphores();
void FreeEntitySemaphores();
Expand All @@ -79,6 +80,6 @@ void StopPhysicsThread();
void *UpdateEntityPhysics();
void UpdatePlayerPhysics();
void UpdateEnemyPhysics(Entity *enemyPtr);
void StartRenderingThread();
void StartRenderingThread(int fixedScreen_);
void StopRenderingThread();
void *RenderEntities();
2 changes: 1 addition & 1 deletion include/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern HANDLE consoleOutputHandle;
extern SMALL_RECT consoleRect;
extern CHAR_INFO *consoleBuffer;

void InitConsoleRenderer(int width, int height);
void InitConsoleRenderer(int consoleWidth_, int consoleHeight_);
void FreeConsoleRenderer();
void SetCharOnPosition(int x, int y, char c, unsigned short color);
void PrintStringOnPosition(char *s, unsigned short color, int x, int y);
Expand Down
34 changes: 27 additions & 7 deletions include/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

#include "../include/core.h"

#define MAX_TEXT_STRLEN 720
#define MAX_BUTTON_STRLEN 9
#define MAX_TEXT_STRLEN 1024
#define MAX_BUTTON_STRLEN 32
#define MAX_TEXTS 4
#define MAX_BUTTONS 3
#define VERSION "2.15"
#define MAX_BUTTONS 16
#define VERSION "2.16"

enum Alignment
{
Expand Down Expand Up @@ -53,14 +53,34 @@ typedef struct
} Interface;

extern Interface mainMenu;
extern Interface startMenu;
extern Interface infoMenu;
extern Interface gameplay;
extern Interface pause;
extern Interface gameover;
extern int interfaceKeyLock;

void CalculateAlignedPosition(int *x, int *y, int sizeX, int sizeY, enum Alignment alignment);
void InterfaceBehaviour(Interface *interfaceIn);
void CalculateAlignedPosition(char* string, int *x, int *y, enum Alignment alignment);
Text CreateText(char content[MAX_TEXT_STRLEN],
unsigned short color,
int x,
int y,
int update,
enum Alignment alignment);
Button CreateButton(char content[MAX_BUTTON_STRLEN],
unsigned short color,
int x,
int y,
enum Event event,
int update,
enum Alignment alignment);
void InterfaceBehaviour(Interface *interface_);
void UpdateInterfaces();
void RenderInterface(Interface *interfaceIn);
void RenderInterface(Interface *interface_);
void BuildMainMenuInterface();
void BuildInfoInterface();
void BuildStartInterface();
void BuildGameplayInterface();
void BuildPauseInterface();
void BuildGameoverInterface();
void InitInterface();
6 changes: 6 additions & 0 deletions include/statemachine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

void InitEventStateMachine(float tick_, int consoleWidth_, int consoleHeight_);
void RunEventStateMachine();
void FreeEventStateMachine();
void PlayEventBehaviourWrapper(int worldWidth_, int worldHeight_, int empty_, int fixedScreen_);
4 changes: 3 additions & 1 deletion include/world.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

extern int worldWidth, worldHeight, empty;

double RawNoise(int n);
double Noise(int x, int y, int octave, int seed);
double Interpolate(double a, double b, double x);
double Smooth(double x, double y, int octave, int seed);
double PerlinNoise(double x, double y, double persistence, int octaves, int seed);
void GenerateWorld(int width, int height);
void GenerateWorld(int worldWidth_, int worldHeight_, int empty_);
16 changes: 8 additions & 8 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
release: src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c main.c
release: src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c src/statemachine.c main.c
@echo Building the game with release options.
gcc src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c main.c -o Survive -O3 -s -DNDEBUG -fopenmp
gcc src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c src/statemachine.c main.c -o Survive -O3 -s -DNDEBUG -fopenmp

debug: src/core.c src/utilities.c src/core.c src/graphics.c src/interface.c src/entity.c src/world.c main.c
debug: src/core.c src/utilities.c src/core.c src/graphics.c src/interface.c src/entity.c src/world.c src/statemachine.c main.c
@echo Building the game with debug options.
gcc src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c main.c -o Survive -O3 -g -Wall -fopenmp
gcc src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c src/statemachine.c main.c -o Survive -O3 -g -Wall -fopenmp

dis_release: src/core.c src/utilities.c src/core.c src/graphics.c src/interface.c src/entity.c src/world.c main.c
dis_release: src/core.c src/utilities.c src/core.c src/graphics.c src/interface.c src/entity.c src/world.c src/statemachine.c main.c
@echo Building the game with assembly output.
gcc src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c main.c -o Survive -O3 -s -DNDEBUG -fopenmp
gcc src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c src/statemachine.c main.c -o Survive -O3 -s -DNDEBUG -fopenmp
objdump -drwC -Mintel Survive.exe > Survive.s

dis_debug: src/core.c src/utilities.c src/core.c src/graphics.c src/interface.c src/entity.c src/world.c main.c
dis_debug: src/core.c src/utilities.c src/core.c src/graphics.c src/interface.c src/entity.c src/world.c src/statemachine.c main.c
@echo Building the game with assembly output.
gcc src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c main.c -o Survive -O3 -g -Wall -fopenmp
gcc src/core.c src/utilities.c src/graphics.c src/interface.c src/entity.c src/world.c src/statemachine.c main.c -o Survive -O3 -g -Wall -fopenmp
objdump -drwC -Mintel Survive.exe > Survive.s
4 changes: 2 additions & 2 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ void FreeCore()
pthread_mutex_destroy(&eventMutex);
}

void SetGameEvent(enum Event newEvent, int force)
void SetGameEvent(enum Event event_, int force)
{
if (event == IDLE || force)
{
event = newEvent;
event = event_;
}
}

Expand Down
16 changes: 13 additions & 3 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pthread_t physicsThread;
pthread_t renderingThread;
sem_t behaviourSemaphore;
sem_t physicsSemaphore;
int fixedScreen;

void InitEntitySemaphores()
{
Expand Down Expand Up @@ -704,8 +705,9 @@ void UpdateEnemyPhysics(Entity *enemyPtr)
}
}

void StartRenderingThread()
void StartRenderingThread(int fixedScreen_)
{
fixedScreen = fixedScreen_;
pthread_create(&renderingThread, NULL, RenderEntities, NULL);
}

Expand All @@ -727,8 +729,16 @@ void *RenderEntities()
{
StartChronometer(&renderingFrequency, &renderingInitialTime);

xOffset = (int)(entityMatrix.playerPtr->position[0] - 0.5 * consoleWidth);
yOffset = (int)(entityMatrix.playerPtr->position[1] - 0.5 * consoleHeight);
if (!fixedScreen)
{
xOffset = (int)(entityMatrix.playerPtr->position[0] - 0.5 * consoleWidth);
yOffset = (int)(entityMatrix.playerPtr->position[1] - 0.5 * consoleHeight);
}
else
{
xOffset = 0;
yOffset = 0;
}

for (int i = 0; i < consoleHeight - 1; i++)
{
Expand Down
6 changes: 3 additions & 3 deletions src/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ HANDLE consoleOutputHandle;
SMALL_RECT consoleRect;
CHAR_INFO *consoleBuffer;

void InitConsoleRenderer(int width, int height)
void InitConsoleRenderer(int consoleWidth_, int consoleHeight_)
{
consoleWidth = width > 255 ? 255 : width;
consoleHeight = height > 255 ? 255 : height;
consoleWidth = consoleWidth_ > 255 ? 255 : consoleWidth_;
consoleHeight = consoleHeight_ > 255 ? 255 : consoleHeight_;

consoleOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);

Expand Down
Loading

0 comments on commit 6991b9e

Please sign in to comment.