Day 40 complete.
This commit is contained in:
@@ -1,6 +1,16 @@
|
|||||||
#ifndef CORE_H_SENTRY
|
#ifndef CORE_H_SENTRY
|
||||||
#define CORE_H_SENTRY
|
#define CORE_H_SENTRY
|
||||||
|
|
||||||
|
/* ---- Switches ------------------------------------------------------- */
|
||||||
|
|
||||||
|
#ifndef BUILD_DEBUG
|
||||||
|
#define BUILD_DEBUG 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BUILD_INTERNAL
|
||||||
|
#define BUILD_INTERNAL 1
|
||||||
|
#endif
|
||||||
|
|
||||||
/* ---- Environment Detection ------------------------------------------ */
|
/* ---- Environment Detection ------------------------------------------ */
|
||||||
|
|
||||||
/* Architecture */
|
/* Architecture */
|
||||||
@@ -91,16 +101,6 @@
|
|||||||
#error Unknown compiler . . .
|
#error Unknown compiler . . .
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* ---- Switches ------------------------------------------------------- */
|
|
||||||
|
|
||||||
#ifndef BUILD_DEBUG
|
|
||||||
#define BUILD_DEBUG 1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef BUILD_INTERNAL
|
|
||||||
#define BUILD_INTERNAL 1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* ---- Types ---------------------------------------------------------- */
|
/* ---- Types ---------------------------------------------------------- */
|
||||||
|
|
||||||
#ifndef TRUE
|
#ifndef TRUE
|
||||||
|
|||||||
+76
-20
@@ -40,6 +40,10 @@ SDL_GameController *ControllerHandles[MAX_CONTROLLERS];
|
|||||||
|
|
||||||
static offscreen_buffer GlobalBackbuffer;
|
static offscreen_buffer GlobalBackbuffer;
|
||||||
|
|
||||||
|
static B32 IsFullscreen;
|
||||||
|
static sdl_window_size WindowSize;
|
||||||
|
static sdl_window_position WindowPosition;
|
||||||
|
|
||||||
static S32 str_len(char *str)
|
static S32 str_len(char *str)
|
||||||
{
|
{
|
||||||
S32 count = 0;
|
S32 count = 0;
|
||||||
@@ -130,13 +134,20 @@ void DEBUGPlatformFreeFileMemory(thread_context *Thread, void *Memory)
|
|||||||
free(Memory);
|
free(Memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sdl_window_dimension SDLGetWindowDimension(SDL_Window *Window)
|
static sdl_window_size SDLGetWindowSize(SDL_Window *Window)
|
||||||
{
|
{
|
||||||
sdl_window_dimension Dimension;
|
sdl_window_size Dimension;
|
||||||
SDL_GetWindowSize(Window, &Dimension.Width, &Dimension.Height);
|
SDL_GetWindowSize(Window, &Dimension.Width, &Dimension.Height);
|
||||||
return Dimension;
|
return Dimension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static sdl_window_position SDLGetWindowPosition(SDL_Window *Window)
|
||||||
|
{
|
||||||
|
sdl_window_position Position;
|
||||||
|
SDL_GetWindowPosition(Window, &Position.X, &Position.Y);
|
||||||
|
return Position;
|
||||||
|
}
|
||||||
|
|
||||||
static void SDLResizeTexture(offscreen_buffer *Buffer, SDL_Renderer *Renderer, int Width, int Height)
|
static void SDLResizeTexture(offscreen_buffer *Buffer, SDL_Renderer *Renderer, int Width, int Height)
|
||||||
{
|
{
|
||||||
if(Buffer->Texture)
|
if(Buffer->Texture)
|
||||||
@@ -155,7 +166,7 @@ static void SDLResizeTexture(offscreen_buffer *Buffer, SDL_Renderer *Renderer, i
|
|||||||
Buffer->Pitch = Width * BytesPerPixel;
|
Buffer->Pitch = Width * BytesPerPixel;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Window, SDL_Renderer *Renderer)
|
static void SDLDisplayBufferInWindow(offscreen_buffer *Buffer, SDL_Window *Window, SDL_Renderer *Renderer)
|
||||||
{
|
{
|
||||||
S32 OffsetX = 10;
|
S32 OffsetX = 10;
|
||||||
S32 OffsetY = 10;
|
S32 OffsetY = 10;
|
||||||
@@ -163,15 +174,20 @@ static void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Window
|
|||||||
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
|
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
|
||||||
SDL_RenderClear(Renderer);
|
SDL_RenderClear(Renderer);
|
||||||
|
|
||||||
if(SDL_UpdateTexture(Buffer.Texture, 0, Buffer.Memory, Buffer.Pitch)) {
|
if(SDL_UpdateTexture(Buffer->Texture, 0, Buffer->Memory, Buffer->Pitch)) {
|
||||||
/* TODO: Do something about this error! */
|
/* TODO: Do something about this error! */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: We temporarily introduce the target rectangle to avoid
|
/* TODO: We temporarily introduce the target rectangle to avoid stretching the canvas. */
|
||||||
* stretching the canvas. */
|
|
||||||
/* SDL_RenderCopy(Renderer, Buffer.Texture, 0, 0); */
|
/* SDL_RenderCopy(Renderer, Buffer.Texture, 0, 0); */
|
||||||
|
sdl_window_size WindowSize = SDLGetWindowSize(Window);
|
||||||
|
if(WindowSize.Width >= Buffer->Width*2 && WindowSize.Height >= Buffer->Height*2) {
|
||||||
|
SDL_Rect dest_rect = { OffsetX, OffsetY, Buffer->Width*2, Buffer->Height*2 };
|
||||||
|
SDL_RenderCopy(Renderer, Buffer->Texture, 0, (const SDL_Rect *)(&dest_rect));
|
||||||
|
} else {
|
||||||
SDL_Rect dest_rect = { OffsetX, OffsetY, RESOLUTION_WIDTH, RESOLUTION_HEIGHT };
|
SDL_Rect dest_rect = { OffsetX, OffsetY, RESOLUTION_WIDTH, RESOLUTION_HEIGHT };
|
||||||
SDL_RenderCopy(Renderer, Buffer.Texture, 0, (const SDL_Rect *)(&dest_rect));
|
SDL_RenderCopy(Renderer, Buffer->Texture, 0, (const SDL_Rect *)(&dest_rect));
|
||||||
|
}
|
||||||
SDL_RenderPresent(Renderer);
|
SDL_RenderPresent(Renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,11 +300,38 @@ static void HandleEvent(SDL_Event *Event)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SDLProcessMessages(sdl_state *SDLState,
|
// NOTE: On XFCE4 (tested on X11), the window moves slightly lower than the set position, for whatever reason.
|
||||||
game_controller_input *KeyboardController)
|
// Maybe, we need to redraw inside of the window atleast once before repositioning?
|
||||||
|
// This issue does not happen on MacOS.
|
||||||
|
static void SDLToggleFullscreen(SDL_Window *Window)
|
||||||
|
{
|
||||||
|
if(IsFullscreen) {
|
||||||
|
if(SDL_SetWindowFullscreen(Window, 0) == 0) {
|
||||||
|
SDL_SetWindowSize(Window, WindowSize.Width, WindowSize.Height);
|
||||||
|
SDL_SetWindowPosition(Window, WindowPosition.X, WindowPosition.Y);
|
||||||
|
IsFullscreen = FALSE;
|
||||||
|
} else {
|
||||||
|
/* TODO: This didn't work . . . SDL_GetError() */
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
WindowSize = SDLGetWindowSize(Window);
|
||||||
|
WindowPosition = SDLGetWindowPosition(Window);
|
||||||
|
|
||||||
|
if(SDL_SetWindowFullscreen(Window, SDL_WINDOW_FULLSCREEN_DESKTOP) == 0) {
|
||||||
|
IsFullscreen = TRUE;
|
||||||
|
} else {
|
||||||
|
/* TODO: This didn't work . . . SDL_GetError() */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SDLProcessMessages(sdl_state *SDLState, game_controller_input *KeyboardController)
|
||||||
{
|
{
|
||||||
SDL_Event Event;
|
SDL_Event Event;
|
||||||
while(SDL_PollEvent(&Event)) {
|
while(SDL_PollEvent(&Event)) {
|
||||||
|
SDL_Window *Window = SDL_GetWindowFromID(Event.window.windowID);
|
||||||
|
SDL_Renderer *Renderer = SDL_GetRenderer(Window);
|
||||||
|
|
||||||
switch(Event.type) {
|
switch(Event.type) {
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
case SDL_KEYUP: {
|
case SDL_KEYUP: {
|
||||||
@@ -338,12 +381,17 @@ static void SDLProcessMessages(sdl_state *SDLState,
|
|||||||
* to see SDL_QUIT event occur before our
|
* to see SDL_QUIT event occur before our
|
||||||
* keybind. */
|
* keybind. */
|
||||||
B32 AltKeyWasDown = (Event.key.keysym.mod & KMOD_ALT);
|
B32 AltKeyWasDown = (Event.key.keysym.mod & KMOD_ALT);
|
||||||
|
|
||||||
if(KeyCode == SDLK_F4 && AltKeyWasDown)
|
if(KeyCode == SDLK_F4 && AltKeyWasDown)
|
||||||
GlobalRunning = FALSE;
|
GlobalRunning = FALSE;
|
||||||
#if BUILD_INTERNAL
|
#if BUILD_INTERNAL
|
||||||
if(KeyCode == SDLK_ESCAPE)
|
if(KeyCode == SDLK_ESCAPE)
|
||||||
GlobalRunning = FALSE;
|
GlobalRunning = FALSE;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if((KeyCode == SDLK_RETURN) && AltKeyWasDown) {
|
||||||
|
SDLToggleFullscreen(Window);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
@@ -551,14 +599,16 @@ S32 main(S32 argc, char *argv[])
|
|||||||
/* NOTE: Floating windows are not supported oficially by Wayland
|
/* NOTE: Floating windows are not supported oficially by Wayland
|
||||||
* protocol. However, SDL_WINDOW_ALWAYS_ON_TOP does work on KDE
|
* protocol. However, SDL_WINDOW_ALWAYS_ON_TOP does work on KDE
|
||||||
* under Wayland. I have not tested GNOME. */
|
* under Wayland. I have not tested GNOME. */
|
||||||
SDL_Window *Window = SDL_CreateWindow("Handmade Hero", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, RESOLUTION_WIDTH+20, RESOLUTION_HEIGHT+20, SDL_WINDOW_RESIZABLE/* | SDL_WINDOW_ALWAYS_ON_TOP*/);
|
/* NOTE: We create the window hidden at first. This is so that we do not get a window outline
|
||||||
|
* that shows up whilst it is waiting for the rendered to be created. We want to display
|
||||||
|
* the window once everything is ready. */
|
||||||
|
SDL_Window *Window = SDL_CreateWindow("Handmade Hero", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, RESOLUTION_WIDTH+20, RESOLUTION_HEIGHT+20, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE/* | SDL_WINDOW_ALWAYS_ON_TOP*/);
|
||||||
|
|
||||||
if(Window) {
|
if(Window) {
|
||||||
SDL_DisplayMode display_mode;
|
SDL_DisplayMode display_mode;
|
||||||
SDL_GetCurrentDisplayMode(0, &display_mode);
|
SDL_GetCurrentDisplayMode(0, &display_mode);
|
||||||
|
|
||||||
SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_PRESENTVSYNC);
|
SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_PRESENTVSYNC);
|
||||||
SDL_SetWindowPosition(Window, (display_mode.w - RESOLUTION_WIDTH-30), (display_mode.h - RESOLUTION_HEIGHT-30));
|
|
||||||
|
|
||||||
/* NOTE: For future reference, the custom window bar should use
|
/* NOTE: For future reference, the custom window bar should use
|
||||||
* SDL_SetWindowHitTest to define areas that will be used to
|
* SDL_SetWindowHitTest to define areas that will be used to
|
||||||
@@ -571,18 +621,17 @@ S32 main(S32 argc, char *argv[])
|
|||||||
* its actual size. */
|
* its actual size. */
|
||||||
/* SDL_RenderPresent(Renderer) is enough to display the window. */
|
/* SDL_RenderPresent(Renderer) is enough to display the window. */
|
||||||
if(Renderer) {
|
if(Renderer) {
|
||||||
#if BUILD_DEBUG
|
|
||||||
void *BaseAddress = (void *)TB(2);
|
|
||||||
#else
|
|
||||||
void *BaseAddress = (void *)(0);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SDLSetProgramIcon(Window);
|
SDLSetProgramIcon(Window);
|
||||||
#if 0
|
|
||||||
|
// TODO: Make it a global.
|
||||||
|
SDL_Cursor *PointerCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
|
||||||
|
SDL_SetCursor(PointerCursor);
|
||||||
|
#if !BUILD_INTERNAL
|
||||||
if(SDL_ShowCursor(SDL_DISABLE) < 0) {
|
if(SDL_ShowCursor(SDL_DISABLE) < 0) {
|
||||||
/* TODO: This didn't work . . . SDL_GetError() */
|
/* TODO: This didn't work . . . SDL_GetError() */
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
SDL_SetWindowPosition(Window, (display_mode.w - RESOLUTION_WIDTH-20), (display_mode.h - RESOLUTION_HEIGHT-20));
|
||||||
|
|
||||||
GlobalRunning = TRUE;
|
GlobalRunning = TRUE;
|
||||||
|
|
||||||
@@ -595,7 +644,6 @@ S32 main(S32 argc, char *argv[])
|
|||||||
SoundOutput.SamplesPerSecond = 48000;
|
SoundOutput.SamplesPerSecond = 48000;
|
||||||
SoundOutput.BytesPerSample = sizeof(S16) * 2;
|
SoundOutput.BytesPerSample = sizeof(S16) * 2;
|
||||||
SoundOutput.SecondaryBufferSize = SoundOutput.SamplesPerSecond * SoundOutput.BytesPerSample;
|
SoundOutput.SecondaryBufferSize = SoundOutput.SamplesPerSecond * SoundOutput.BytesPerSample;
|
||||||
SoundOutput.tSine = 0;
|
|
||||||
SoundOutput.LatencySampleCount = SoundOutput.SamplesPerSecond / 15;
|
SoundOutput.LatencySampleCount = SoundOutput.SamplesPerSecond / 15;
|
||||||
|
|
||||||
SDLInitSound(SoundOutput.SamplesPerSecond, SoundOutput.SamplesPerSecond * SoundOutput.BytesPerSample / 60);
|
SDLInitSound(SoundOutput.SamplesPerSecond, SoundOutput.SamplesPerSecond * SoundOutput.BytesPerSample / 60);
|
||||||
@@ -607,6 +655,12 @@ S32 main(S32 argc, char *argv[])
|
|||||||
/* NOTE: calloc auto clears to zero */
|
/* NOTE: calloc auto clears to zero */
|
||||||
/* SDLClearSoundBuffer(&SoundOutput); */
|
/* SDLClearSoundBuffer(&SoundOutput); */
|
||||||
|
|
||||||
|
#if BUILD_DEBUG
|
||||||
|
void *BaseAddress = (void *)TB(2);
|
||||||
|
#else
|
||||||
|
void *BaseAddress = (void *)(0);
|
||||||
|
#endif
|
||||||
|
|
||||||
game_memory GameMemory;
|
game_memory GameMemory;
|
||||||
memset(&GameMemory, 0, sizeof(GameMemory));
|
memset(&GameMemory, 0, sizeof(GameMemory));
|
||||||
GameMemory.PermanentStorageSize = MB(64);
|
GameMemory.PermanentStorageSize = MB(64);
|
||||||
@@ -641,6 +695,8 @@ S32 main(S32 argc, char *argv[])
|
|||||||
|
|
||||||
sdl_game_code Game = SDLLoadGameCode(SDLState.DLLPath);
|
sdl_game_code Game = SDLLoadGameCode(SDLState.DLLPath);
|
||||||
|
|
||||||
|
SDL_ShowWindow(Window); // NOTE: Everything is ready; display the window.
|
||||||
|
|
||||||
while(GlobalRunning) {
|
while(GlobalRunning) {
|
||||||
NewInput->dtForFrame = TargetSecondsPerFrame;
|
NewInput->dtForFrame = TargetSecondsPerFrame;
|
||||||
|
|
||||||
@@ -789,7 +845,7 @@ S32 main(S32 argc, char *argv[])
|
|||||||
|
|
||||||
U64 EndCounter = SDLGetWallClock();
|
U64 EndCounter = SDLGetWallClock();
|
||||||
|
|
||||||
SDLDisplayBufferInWindow(GlobalBackbuffer, Window, Renderer);
|
SDLDisplayBufferInWindow(&GlobalBackbuffer, Window, Renderer);
|
||||||
|
|
||||||
F64 MSPerFrame = 1000.0 * SDLGetSecondsElapsed(LastCounter, EndCounter);
|
F64 MSPerFrame = 1000.0 * SDLGetSecondsElapsed(LastCounter, EndCounter);
|
||||||
F64 FPS = 1000.0 / MSPerFrame;
|
F64 FPS = 1000.0 / MSPerFrame;
|
||||||
|
|||||||
+7
-3
@@ -6,10 +6,15 @@
|
|||||||
#include "core.h"
|
#include "core.h"
|
||||||
#include "handmade.h"
|
#include "handmade.h"
|
||||||
|
|
||||||
typedef struct sdl_window_dimension {
|
typedef struct sdl_window_size {
|
||||||
S32 Width;
|
S32 Width;
|
||||||
S32 Height;
|
S32 Height;
|
||||||
} sdl_window_dimension;
|
} sdl_window_size;
|
||||||
|
|
||||||
|
typedef struct sdl_window_position {
|
||||||
|
S32 X;
|
||||||
|
S32 Y;
|
||||||
|
} sdl_window_position;
|
||||||
|
|
||||||
typedef struct offscreen_buffer {
|
typedef struct offscreen_buffer {
|
||||||
/* Pixels are always 32-bit and have the bytes in BGRX
|
/* Pixels are always 32-bit and have the bytes in BGRX
|
||||||
@@ -25,7 +30,6 @@ typedef struct sdl_sound_output {
|
|||||||
S32 SamplesPerSecond;
|
S32 SamplesPerSecond;
|
||||||
S32 BytesPerSample;
|
S32 BytesPerSample;
|
||||||
S32 SecondaryBufferSize;
|
S32 SecondaryBufferSize;
|
||||||
F32 tSine;
|
|
||||||
S32 LatencySampleCount;
|
S32 LatencySampleCount;
|
||||||
void *Samples;
|
void *Samples;
|
||||||
} sdl_sound_output;
|
} sdl_sound_output;
|
||||||
|
|||||||
Reference in New Issue
Block a user