Wire App into Main.cpp

This commit is contained in:
Edgaru089 2024-03-04 15:05:21 +08:00
parent abb9eb569f
commit 0239b8ff68
8 changed files with 90 additions and 17 deletions

2
App.c
View File

@ -15,6 +15,8 @@ App *app_NewApp() {
app->player = player_NewSystem(app); app->player = player_NewSystem(app);
app->entity = entity_NewSystem(app); app->entity = entity_NewSystem(app);
app->wantQuit = false;
return app; return app;
} }

11
App.h
View File

@ -6,12 +6,18 @@
#include "Input.h" #include "Input.h"
#include "Types.h" #include "Types.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _App { typedef struct _App {
System_Physics *physics; System_Physics *physics;
System_Player *player; System_Player *player;
System_Input *input; System_Input *input;
System_Entity *entity; System_Entity *entity;
bool wantQuit;
} App; } App;
App *app_NewApp(); App *app_NewApp();
@ -19,3 +25,8 @@ void app_DeleteApp(App *app);
void app_Advance(App *app, Duration deltaTime); void app_Advance(App *app, Duration deltaTime);
void app_Render(); void app_Render();
#ifdef __cplusplus
}
#endif

View File

@ -3,5 +3,9 @@
#include <graphics.h> #include <graphics.h>
extern "C" {
void app_Render() { void app_Render() {
} }
}

23
Input.c
View File

@ -3,9 +3,21 @@
#include "App.h" #include "App.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <windows.h> #include <windows.h>
const char *input_KeyNames[input_Key_Count] = {
"Left",
"Right",
"Up",
"Down",
"Jump",
"Attack",
"Spell",
"Use",
"Escape"};
void input_SetDefaultKeymap(System_Input *sys) { void input_SetDefaultKeymap(System_Input *sys) {
sys->systemKeymap[input_Key_Up] = 'W'; sys->systemKeymap[input_Key_Up] = 'W';
sys->systemKeymap[input_Key_Left] = 'A'; sys->systemKeymap[input_Key_Left] = 'A';
@ -15,6 +27,7 @@ void input_SetDefaultKeymap(System_Input *sys) {
sys->systemKeymap[input_Key_Attack] = 'J'; sys->systemKeymap[input_Key_Attack] = 'J';
sys->systemKeymap[input_Key_Spell] = 'K'; sys->systemKeymap[input_Key_Spell] = 'K';
sys->systemKeymap[input_Key_Use] = 'L'; sys->systemKeymap[input_Key_Use] = 'L';
sys->systemKeymap[input_Key_Escape] = VK_ESCAPE;
} }
System_Input *input_NewSystem(App *super) { System_Input *input_NewSystem(App *super) {
@ -36,9 +49,10 @@ void input_Advance(System_Input *sys) {
// Checks the most significiant bit (of the SHORT returned) // Checks the most significiant bit (of the SHORT returned)
if ((GetAsyncKeyState(sys->systemKeymap[i]) & 0x8000) != 0) { if ((GetAsyncKeyState(sys->systemKeymap[i]) & 0x8000) != 0) {
// Pressed // Pressed
if (sys->keys[i] == JustReleased || sys->keys[i] == Released) if (sys->keys[i] == JustReleased || sys->keys[i] == Released) {
fprintf(stderr, "[input_Advance] Key %s pressed\n", input_KeyNames[i]);
sys->keys[i] = JustPressed; sys->keys[i] = JustPressed;
else } else
sys->keys[i] = Pressed; sys->keys[i] = Pressed;
} else { } else {
// Released // Released
@ -48,4 +62,9 @@ void input_Advance(System_Input *sys) {
sys->keys[i] = Released; sys->keys[i] = Released;
} }
} }
if (sys->keys[input_Key_Escape] == JustPressed) {
fprintf(stderr, "[input_Advance] Let's quit now!\n");
sys->super->wantQuit = true;
}
} }

11
Input.h
View File

@ -19,19 +19,12 @@ typedef enum {
input_Key_Attack, input_Key_Attack,
input_Key_Spell, input_Key_Spell,
input_Key_Use, input_Key_Use,
input_Key_Escape,
input_Key_Count input_Key_Count
} input_Key; } input_Key;
// Names for input_Key // Names for input_Key
const char *input_KeyNames[input_Key_Count] = { extern const char *input_KeyNames[input_Key_Count];
"Left",
"Right",
"Up",
"Down",
"Jump",
"Attack",
"Spell",
"Use"};
// States a key might in // States a key might in

View File

@ -1,17 +1,37 @@
#include <cwchar>
#include <graphics.h> #include <graphics.h>
#include <stdio.h> #include <stdio.h>
#include "App.h"
#include "Types.h" #include "Types.h"
int main() { int main() {
TimePoint startup = time_Now(); TimePoint startup = time_Now();
TimePoint lastFrame, lastUpdate, frameCounter;
lastFrame = lastUpdate = frameCounter = time_Now();
int frameCount = 0;
initgraph(1280, 720);
App *app = app_NewApp();
while (!app->wantQuit) {
if (time_Since(frameCounter).microseconds >= 1000000) { // 1 sec
Duration d = time_Reset(&frameCounter);
fprintf(stderr, "[Main] %d frames in the last %.4lf seconds\n", frameCount, duration_Seconds(d));
frameCount = 0;
}
frameCount++;
app_Advance(app, time_Reset(&lastUpdate));
app_Render();
Duration toSleep = {.microseconds = 1000000 / 30 - time_Reset(&lastFrame).microseconds};
duration_Sleep(toSleep);
}
initgraph(640, 480);
circle(200, 200, 100);
Sleep(5000);
closegraph(); closegraph();
printf("%.6lf seconds has elapsed\n", duration_Seconds(time_Since(startup))); printf("%.6lf seconds has elapsed\n", duration_Seconds(time_Since(startup)));
return 0; return 0;
} }

21
Types.c
View File

@ -1,6 +1,8 @@
#include "Types.h" #include "Types.h"
#include <math.h>
#include <windows.h> #include <windows.h>
#include <timeapi.h>
static inline double dmin(double x, double y) { static inline double dmin(double x, double y) {
return x < y ? x : y; return x < y ? x : y;
@ -75,6 +77,19 @@ Box2 box2_OffsetY(Box2 box, double offsetY) {
static double freqInverse = 0.0; static double freqInverse = 0.0;
void duration_Sleep(const Duration t) {
if (t.microseconds <= 0)
return;
// https://learn.microsoft.com/zh-cn/windows/win32/api/timeapi/nf-timeapi-timebeginperiod
// timeBeginPeriod() and friends
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
timeBeginPeriod(tc.wPeriodMin);
Sleep((DWORD)(round(duration_Milliseconds(t)))); // Only millisecond precision. Sad
timeEndPeriod(tc.wPeriodMin);
}
TimePoint time_Now() { TimePoint time_Now() {
// Reference: https://github.com/SFML/SFML/blob/2.6.x/src/SFML/System/Win32/ClockImpl.cpp // Reference: https://github.com/SFML/SFML/blob/2.6.x/src/SFML/System/Win32/ClockImpl.cpp
@ -98,3 +113,9 @@ Duration time_Difference(TimePoint now, TimePoint prev) {
Duration d = {.microseconds = now.microseconds - prev.microseconds}; Duration d = {.microseconds = now.microseconds - prev.microseconds};
return d; return d;
} }
Duration time_Reset(TimePoint *prev) {
TimePoint now = time_Now();
Duration d = time_Difference(now, *prev);
*prev = now;
return d;
}

View File

@ -43,10 +43,12 @@ typedef struct {
int64_t microseconds; int64_t microseconds;
} Duration; } Duration;
static inline double duration_Seconds(const Duration t) { return ((double)t.microseconds) / 1000.0 / 1000.0; } static inline double duration_Seconds(const Duration t) { return ((double)t.microseconds) / 1000000.0; }
static inline double duration_Milliseconds(const Duration t) { return ((double)t.microseconds) / 1000.0; } static inline double duration_Milliseconds(const Duration t) { return ((double)t.microseconds) / 1000.0; }
// This function has a precision of at most 1ms under Windows. Sad
void duration_Sleep(const Duration t);
// A Time point since the start of the program. // A Time point, counted since a fixed point in the past.
typedef struct { typedef struct {
int64_t microseconds; int64_t microseconds;
} TimePoint; } TimePoint;
@ -54,6 +56,7 @@ typedef struct {
TimePoint time_Now(); TimePoint time_Now();
Duration time_Since(TimePoint prev); Duration time_Since(TimePoint prev);
Duration time_Difference(TimePoint now, TimePoint prev); Duration time_Difference(TimePoint now, TimePoint prev);
Duration time_Reset(TimePoint *prev);
// 1e-6 // 1e-6
extern const double EPS; extern const double EPS;