Clean up after core.h updates.
This commit is contained in:
+9
-7
@@ -1,8 +1,10 @@
|
|||||||
#include "handmade.h"
|
#include "handmade.h"
|
||||||
|
|
||||||
internal void GameOutputSound(game_sound_output_buffer *SoundBuffer, S32 ToneHz)
|
#include <math.h>
|
||||||
|
|
||||||
|
static void GameOutputSound(game_sound_output_buffer *SoundBuffer, S32 ToneHz)
|
||||||
{
|
{
|
||||||
local_persist F32 tSine;
|
static F32 tSine;
|
||||||
S16 ToneVolume = 8000;
|
S16 ToneVolume = 8000;
|
||||||
S32 WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz;
|
S32 WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz;
|
||||||
|
|
||||||
@@ -13,11 +15,11 @@ internal void GameOutputSound(game_sound_output_buffer *SoundBuffer, S32 ToneHz)
|
|||||||
*SampleOut++ = SampleValue;
|
*SampleOut++ = SampleValue;
|
||||||
*SampleOut++ = SampleValue;
|
*SampleOut++ = SampleValue;
|
||||||
|
|
||||||
tSine += 2.0f * pi32 * 1.0f / (F32)WavePeriod;
|
tSine += 2.0f * PI * 1.0f / (F32)WavePeriod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32 YOffset)
|
static void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32 YOffset)
|
||||||
{
|
{
|
||||||
S32 Width = Buffer->Width;
|
S32 Width = Buffer->Width;
|
||||||
S32 Height = Buffer->Height;
|
S32 Height = Buffer->Height;
|
||||||
@@ -35,9 +37,9 @@ internal void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S3
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer)
|
static void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer)
|
||||||
{
|
{
|
||||||
Assert((&Input->Controllers[0].Terminator - &Input->Controllers[0].Buttons[0]) == ArrayCount(Input->Controllers[0].Buttons));
|
Assert((&Input->Controllers[0].Terminator - &Input->Controllers[0].Buttons[0]) == ARRAY_SIZE(Input->Controllers[0].Buttons));
|
||||||
Assert(sizeof(game_state) <= Memory->PermanentStorageSize);
|
Assert(sizeof(game_state) <= Memory->PermanentStorageSize);
|
||||||
|
|
||||||
game_state *GameState = (game_state *)Memory->PermanentStorage;
|
game_state *GameState = (game_state *)Memory->PermanentStorage;
|
||||||
@@ -58,7 +60,7 @@ internal void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_o
|
|||||||
// GameState.GreenOffset;
|
// GameState.GreenOffset;
|
||||||
// GameState.BlueOffset;
|
// GameState.BlueOffset;
|
||||||
|
|
||||||
for(U32 ControllerIndex = 0; ControllerIndex < ArrayCount(Input->Controllers); ControllerIndex++) {
|
for(U32 ControllerIndex = 0; ControllerIndex < ARRAY_SIZE(Input->Controllers); ControllerIndex++) {
|
||||||
game_controller_input *Controller = GetController(Input, ControllerIndex);
|
game_controller_input *Controller = GetController(Input, ControllerIndex);
|
||||||
if(Controller->IsAnalog) {
|
if(Controller->IsAnalog) {
|
||||||
GameState->GreenOffset += (int)(4.0f*(Controller->StickAverageY));
|
GameState->GreenOffset += (int)(4.0f*(Controller->StickAverageY));
|
||||||
|
|||||||
+5
-5
@@ -13,9 +13,9 @@ typedef struct debug_read_file_result {
|
|||||||
void *Contents;
|
void *Contents;
|
||||||
} debug_read_file_result;
|
} debug_read_file_result;
|
||||||
|
|
||||||
internal debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename);
|
static debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename);
|
||||||
internal B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory);
|
static B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory);
|
||||||
internal void DEBUGPlatformFreeFileMemory(void *Memory);
|
static void DEBUGPlatformFreeFileMemory(void *Memory);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -81,7 +81,7 @@ typedef struct game_input {
|
|||||||
|
|
||||||
inline game_controller_input *GetController(game_input *Input, S32 ControllerIndex)
|
inline game_controller_input *GetController(game_input *Input, S32 ControllerIndex)
|
||||||
{
|
{
|
||||||
Assert((U32)ControllerIndex < ArrayCount(Input->Controllers));
|
Assert((U32)ControllerIndex < ARRAY_SIZE(Input->Controllers));
|
||||||
game_controller_input *Result = &(Input->Controllers[ControllerIndex]);
|
game_controller_input *Result = &(Input->Controllers[ControllerIndex]);
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
@@ -101,7 +101,7 @@ typedef struct game_memory {
|
|||||||
// F32 SecondsElapsed;
|
// F32 SecondsElapsed;
|
||||||
// } game_clocks;
|
// } game_clocks;
|
||||||
|
|
||||||
internal void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer);
|
static void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer);
|
||||||
|
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
|||||||
+54
-51
@@ -1,10 +1,12 @@
|
|||||||
#include "base_core.h"
|
#include "core.h"
|
||||||
#include "sdl_handmade.h"
|
#include "sdl_handmade.h"
|
||||||
#include "handmade.cpp"
|
#include "handmade.cpp"
|
||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <sys/mman.h> // mmap
|
#include <sys/mman.h> // mmap
|
||||||
#include <stdlib.h> // malloc, calloc
|
#include <stdlib.h> // malloc, calloc
|
||||||
|
#include <string.h> // memset
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h> // fstat
|
#include <sys/stat.h> // fstat
|
||||||
@@ -16,24 +18,24 @@
|
|||||||
#include <x86intrin.h>
|
#include <x86intrin.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
global bool GlobalRunning;
|
static bool GlobalRunning;
|
||||||
|
|
||||||
global U64 GlobalPerfCountFrequency;
|
static U64 GlobalPerfCountFrequency;
|
||||||
|
|
||||||
#define CONTROLLER_LEFT_THUMB_DEADZONE 8000
|
#define CONTROLLER_LEFT_THUMB_DEADZONE 8000
|
||||||
#define MAX_CONTROLLERS 4
|
#define MAX_CONTROLLERS 4
|
||||||
SDL_GameController *ControllerHandles[MAX_CONTROLLERS];
|
SDL_GameController *ControllerHandles[MAX_CONTROLLERS];
|
||||||
|
|
||||||
global offscreen_buffer GlobalBackbuffer;
|
static offscreen_buffer GlobalBackbuffer;
|
||||||
|
|
||||||
internal U32 SafeTruncateUInt64(U64 Value)
|
static U32 SafeTruncateUInt64(U64 Value)
|
||||||
{
|
{
|
||||||
Assert(Value <= 0xFFFFFFFF);
|
Assert(Value <= 0xFFFFFFFF);
|
||||||
U32 Result = (U32)Value;
|
U32 Result = (U32)Value;
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename)
|
static debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename)
|
||||||
{
|
{
|
||||||
debug_read_file_result Result = {0};
|
debug_read_file_result Result = {0};
|
||||||
S32 FileHandle = open(Filename, O_RDONLY);
|
S32 FileHandle = open(Filename, O_RDONLY);
|
||||||
@@ -75,7 +77,7 @@ internal debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory)
|
static B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory)
|
||||||
{
|
{
|
||||||
S32 FileHandle = open(Filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
S32 FileHandle = open(Filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||||
if(FileHandle == -1)
|
if(FileHandle == -1)
|
||||||
@@ -98,21 +100,21 @@ internal B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void DEBUGPlatformFreeFileMemory(void *Memory)
|
static void DEBUGPlatformFreeFileMemory(void *Memory)
|
||||||
{
|
{
|
||||||
if(Memory)
|
if(Memory)
|
||||||
free(Memory);
|
free(Memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sdl_window_dimension SDLGetWindowDimension(SDL_Window *Window)
|
static sdl_window_dimension SDLGetWindowDimension(SDL_Window *Window)
|
||||||
{
|
{
|
||||||
sdl_window_dimension Dimension;
|
sdl_window_dimension Dimension;
|
||||||
SDL_GetWindowSize(Window, &Dimension.Width, &Dimension.Height);
|
SDL_GetWindowSize(Window, &Dimension.Width, &Dimension.Height);
|
||||||
return Dimension;
|
return Dimension;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal 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)
|
||||||
SDL_DestroyTexture(Buffer->Texture);
|
SDL_DestroyTexture(Buffer->Texture);
|
||||||
@@ -130,7 +132,7 @@ internal void SDLResizeTexture(offscreen_buffer *Buffer, SDL_Renderer *Renderer,
|
|||||||
Buffer->Pitch = Width * BytesPerPixel;
|
Buffer->Pitch = Width * BytesPerPixel;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Window, SDL_Renderer *Renderer)
|
static void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Window, SDL_Renderer *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!
|
||||||
@@ -140,7 +142,7 @@ internal void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Wind
|
|||||||
SDL_RenderPresent(Renderer);
|
SDL_RenderPresent(Renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SDLInitControllers()
|
static void SDLInitControllers()
|
||||||
{
|
{
|
||||||
int MaxJoysticks = SDL_NumJoysticks();
|
int MaxJoysticks = SDL_NumJoysticks();
|
||||||
int ControllerIndex = 0;
|
int ControllerIndex = 0;
|
||||||
@@ -155,7 +157,7 @@ internal void SDLInitControllers()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
internal void SDLDeinitControllers()
|
static void SDLDeinitControllers()
|
||||||
{
|
{
|
||||||
for(int ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) {
|
for(int ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) {
|
||||||
if (ControllerHandles[ControllerIndex])
|
if (ControllerHandles[ControllerIndex])
|
||||||
@@ -165,7 +167,7 @@ internal void SDLDeinitControllers()
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
internal void SDLInitSound(S32 SamplesPerSecond, S32 BufferSize)
|
static void SDLInitSound(S32 SamplesPerSecond, S32 BufferSize)
|
||||||
{
|
{
|
||||||
SDL_AudioSpec AudioSettings = {0};
|
SDL_AudioSpec AudioSettings = {0};
|
||||||
|
|
||||||
@@ -181,31 +183,31 @@ internal void SDLInitSound(S32 SamplesPerSecond, S32 BufferSize)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SDLFillSoundBuffer(sdl_sound_output *SoundOutput, S32 BytesToWrite)
|
static void SDLFillSoundBuffer(sdl_sound_output *SoundOutput, S32 BytesToWrite)
|
||||||
{
|
{
|
||||||
SDL_QueueAudio(1, SoundOutput->Samples, BytesToWrite);
|
SDL_QueueAudio(1, SoundOutput->Samples, BytesToWrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SDLClearSoundBuffer(sdl_sound_output *SoundOutput)
|
static void SDLClearSoundBuffer(sdl_sound_output *SoundOutput)
|
||||||
{
|
{
|
||||||
memset(SoundOutput->Samples, 0, SoundOutput->SecondaryBufferSize);
|
memset(SoundOutput->Samples, 0, SoundOutput->SecondaryBufferSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SDLProcessKeyboardMessage(game_button_state *NewState, bool IsDown)
|
static void SDLProcessKeyboardMessage(game_button_state *NewState, bool IsDown)
|
||||||
{
|
{
|
||||||
Assert(NewState->EndedDown != IsDown);
|
Assert(NewState->EndedDown != IsDown);
|
||||||
NewState->EndedDown = IsDown;
|
NewState->EndedDown = IsDown;
|
||||||
++NewState->HalfTransitionCount;
|
++NewState->HalfTransitionCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SDLProcessInputDigitalButton(SDL_GameController *Controller, game_button_state *OldState,
|
static void SDLProcessInputDigitalButton(SDL_GameController *Controller, game_button_state *OldState,
|
||||||
SDL_GameControllerButton Button, game_button_state *NewState)
|
SDL_GameControllerButton Button, game_button_state *NewState)
|
||||||
{
|
{
|
||||||
NewState->EndedDown = SDL_GameControllerGetButton(Controller, Button);
|
NewState->EndedDown = SDL_GameControllerGetButton(Controller, Button);
|
||||||
NewState->HalfTransitionCount = (OldState->EndedDown != NewState->EndedDown) ? 1 : 0;
|
NewState->HalfTransitionCount = (OldState->EndedDown != NewState->EndedDown) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void HandleEvent(SDL_Event *Event)
|
static void HandleEvent(SDL_Event *Event)
|
||||||
{
|
{
|
||||||
switch(Event->type) {
|
switch(Event->type) {
|
||||||
case SDL_QUIT: {
|
case SDL_QUIT: {
|
||||||
@@ -229,7 +231,7 @@ internal void HandleEvent(SDL_Event *Event)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SDLProcessMessages(game_controller_input *KeyboardController)
|
static void SDLProcessMessages(game_controller_input *KeyboardController)
|
||||||
{
|
{
|
||||||
SDL_Event Event;
|
SDL_Event Event;
|
||||||
while(SDL_PollEvent(&Event)) {
|
while(SDL_PollEvent(&Event)) {
|
||||||
@@ -286,7 +288,7 @@ internal void SDLProcessMessages(game_controller_input *KeyboardController)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal F32 SDLProcessInputStickValue(F32 Value, S32 DeadZoneThreshold)
|
static float SDLProcessInputStickValue(F32 Value, S32 DeadZoneThreshold)
|
||||||
{
|
{
|
||||||
F32 Result = 0.0f;
|
F32 Result = 0.0f;
|
||||||
if(Value < -DeadZoneThreshold)
|
if(Value < -DeadZoneThreshold)
|
||||||
@@ -297,7 +299,7 @@ internal F32 SDLProcessInputStickValue(F32 Value, S32 DeadZoneThreshold)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define DEFAULT_REFRESH_RATE 60
|
#define DEFAULT_REFRESH_RATE 60
|
||||||
internal S32 SDLGetWindowRefreshRate(SDL_Window *Window)
|
static int SDLGetWindowRefreshRate(SDL_Window *Window)
|
||||||
{
|
{
|
||||||
int DisplayIndex = SDL_GetWindowDisplayIndex(Window);
|
int DisplayIndex = SDL_GetWindowDisplayIndex(Window);
|
||||||
SDL_DisplayMode Mode;
|
SDL_DisplayMode Mode;
|
||||||
@@ -309,19 +311,20 @@ internal S32 SDLGetWindowRefreshRate(SDL_Window *Window)
|
|||||||
return Mode.refresh_rate;
|
return Mode.refresh_rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal U64 SDLGetWallClock()
|
static unsigned long long SDLGetWallClock()
|
||||||
{
|
{
|
||||||
return SDL_GetPerformanceCounter();
|
return SDL_GetPerformanceCounter();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal F32 SDLGetSecondsElapsed(U64 Start, U64 End)
|
// WARNING: Might lose lots of precision because of conversion from 64-bit to float.
|
||||||
|
static float SDLGetSecondsElapsed(unsigned long long Start, unsigned long long End)
|
||||||
{
|
{
|
||||||
return (F32)(End - Start) / (F32)GlobalPerfCountFrequency;
|
return (float)(End - Start) / (float)GlobalPerfCountFrequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
S32 main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_GAMECONTROLLER|SDL_INIT_AUDIO)) {
|
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO)) {
|
||||||
/* TODO: This didn't work . . . */
|
/* TODO: This didn't work . . . */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,9 +382,9 @@ S32 main(int argc, char *argv[])
|
|||||||
|
|
||||||
GlobalPerfCountFrequency = SDL_GetPerformanceFrequency();
|
GlobalPerfCountFrequency = SDL_GetPerformanceFrequency();
|
||||||
|
|
||||||
U64 LastCounter;
|
unsigned long long LastCounter;
|
||||||
#if __x86_64__ || __i386__
|
#if __x86_64__ || __i386__
|
||||||
U64 LastCycleCount;
|
unsigned long long LastCycleCount;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while(GlobalRunning) {
|
while(GlobalRunning) {
|
||||||
@@ -391,9 +394,9 @@ S32 main(int argc, char *argv[])
|
|||||||
LastCycleCount = __rdtsc(); // NOTE: rdtsc reports clock cylces, however it is not meant for really precise profiler work as the value returned is varied.
|
LastCycleCount = __rdtsc(); // NOTE: rdtsc reports clock cylces, however it is not meant for really precise profiler work as the value returned is varied.
|
||||||
#endif // Also, __rdtsc is not available on MacOS ARM; I have not checked MacOS x64.
|
#endif // Also, __rdtsc is not available on MacOS ARM; I have not checked MacOS x64.
|
||||||
|
|
||||||
S32 MonitorRefreshHz = SDLGetWindowRefreshRate(Window);
|
int MonitorRefreshHz = SDLGetWindowRefreshRate(Window);
|
||||||
S32 GameUpdateHz = MonitorRefreshHz / 2;
|
int GameUpdateHz = MonitorRefreshHz / 2;
|
||||||
F32 TargetSecondsPerFrame = 1.0f / (F32)GameUpdateHz;
|
float TargetSecondsPerFrame = 1.0f / (F32)GameUpdateHz;
|
||||||
|
|
||||||
game_controller_input *OldKeyboardController = GetController(OldInput, 0);
|
game_controller_input *OldKeyboardController = GetController(OldInput, 0);
|
||||||
game_controller_input *NewKeyboardController = GetController(NewInput, 0);
|
game_controller_input *NewKeyboardController = GetController(NewInput, 0);
|
||||||
@@ -401,21 +404,21 @@ S32 main(int argc, char *argv[])
|
|||||||
*NewKeyboardController = ZeroController;
|
*NewKeyboardController = ZeroController;
|
||||||
NewKeyboardController->IsConnected = true;
|
NewKeyboardController->IsConnected = true;
|
||||||
|
|
||||||
for(U32 ButtonIndex = 0; ButtonIndex < ArrayCount(NewKeyboardController->Buttons); ButtonIndex++) {
|
for(unsigned int ButtonIndex = 0; ButtonIndex < ARRAY_SIZE(NewKeyboardController->Buttons); ButtonIndex++) {
|
||||||
NewKeyboardController->Buttons[ButtonIndex].EndedDown = OldKeyboardController->Buttons[ButtonIndex].EndedDown;
|
NewKeyboardController->Buttons[ButtonIndex].EndedDown = OldKeyboardController->Buttons[ButtonIndex].EndedDown;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDLProcessMessages(NewKeyboardController);
|
SDLProcessMessages(NewKeyboardController);
|
||||||
|
|
||||||
for(S32 ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) {
|
for(unsigned int ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) {
|
||||||
game_controller_input *OldController = GetController(OldInput, ControllerIndex+1);
|
game_controller_input *OldController = GetController(OldInput, ControllerIndex+1);
|
||||||
game_controller_input *NewController = GetController(NewInput, ControllerIndex+1);
|
game_controller_input *NewController = GetController(NewInput, ControllerIndex+1);
|
||||||
|
|
||||||
if(ControllerHandles[ControllerIndex] != 0 && SDL_GameControllerGetAttached(ControllerHandles[ControllerIndex])) {
|
if(ControllerHandles[ControllerIndex] != 0 && SDL_GameControllerGetAttached(ControllerHandles[ControllerIndex])) {
|
||||||
NewController->IsConnected = true;
|
NewController->IsConnected = true;
|
||||||
|
|
||||||
S16 StickX = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTX);
|
short StickX = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTX);
|
||||||
S16 StickY = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTY);
|
short StickY = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTY);
|
||||||
|
|
||||||
NewController->IsAnalog = true;
|
NewController->IsAnalog = true;
|
||||||
NewController->StickAverageX = SDLProcessInputStickValue((F32)StickX, CONTROLLER_LEFT_THUMB_DEADZONE);
|
NewController->StickAverageX = SDLProcessInputStickValue((F32)StickX, CONTROLLER_LEFT_THUMB_DEADZONE);
|
||||||
@@ -445,7 +448,7 @@ S32 main(int argc, char *argv[])
|
|||||||
NewController->IsAnalog = false;
|
NewController->IsAnalog = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
F32 Threshold = 0.5f;
|
float Threshold = 0.5f;
|
||||||
SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageY < -Threshold) ? 1 : 0], &OldController->MoveUp, SDL_CONTROLLER_BUTTON_A, &NewController->MoveUp);
|
SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageY < -Threshold) ? 1 : 0], &OldController->MoveUp, SDL_CONTROLLER_BUTTON_A, &NewController->MoveUp);
|
||||||
SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageY > Threshold) ? 1 : 0], &OldController->MoveDown, SDL_CONTROLLER_BUTTON_A, &NewController->MoveDown);
|
SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageY > Threshold) ? 1 : 0], &OldController->MoveDown, SDL_CONTROLLER_BUTTON_A, &NewController->MoveDown);
|
||||||
SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageX < -Threshold) ? 1 : 0], &OldController->MoveLeft, SDL_CONTROLLER_BUTTON_A, &NewController->MoveLeft);
|
SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageX < -Threshold) ? 1 : 0], &OldController->MoveLeft, SDL_CONTROLLER_BUTTON_A, &NewController->MoveLeft);
|
||||||
@@ -466,12 +469,12 @@ S32 main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
S32 TargetQueueBytes = SoundOutput.LatencySampleCount * SoundOutput.BytesPerSample;
|
int TargetQueueBytes = SoundOutput.LatencySampleCount * SoundOutput.BytesPerSample;
|
||||||
S32 BytesToWrite = TargetQueueBytes - SDL_GetQueuedAudioSize(1);
|
int BytesToWrite = TargetQueueBytes - SDL_GetQueuedAudioSize(1);
|
||||||
game_sound_output_buffer SoundBuffer;
|
game_sound_output_buffer SoundBuffer;
|
||||||
SoundBuffer.SamplesPerSecond = SoundOutput.SamplesPerSecond;
|
SoundBuffer.SamplesPerSecond = SoundOutput.SamplesPerSecond;
|
||||||
SoundBuffer.SampleCount = BytesToWrite / SoundOutput.BytesPerSample;
|
SoundBuffer.SampleCount = BytesToWrite / SoundOutput.BytesPerSample;
|
||||||
SoundBuffer.Samples = (S16 *)SoundOutput.Samples;
|
SoundBuffer.Samples = (short *)SoundOutput.Samples;
|
||||||
|
|
||||||
game_offscreen_buffer Buffer;
|
game_offscreen_buffer Buffer;
|
||||||
Buffer.Memory = GlobalBackbuffer.Memory;
|
Buffer.Memory = GlobalBackbuffer.Memory;
|
||||||
@@ -481,40 +484,40 @@ S32 main(int argc, char *argv[])
|
|||||||
GameUpdateAndRender(&GameMemory, NewInput, &Buffer, &SoundBuffer);
|
GameUpdateAndRender(&GameMemory, NewInput, &Buffer, &SoundBuffer);
|
||||||
SDLFillSoundBuffer(&SoundOutput, BytesToWrite);
|
SDLFillSoundBuffer(&SoundOutput, BytesToWrite);
|
||||||
|
|
||||||
U64 WorkCounter = SDLGetWallClock();
|
unsigned long WorkCounter = SDLGetWallClock();
|
||||||
F32 WorkSecondsElapsed = SDLGetSecondsElapsed(LastCounter, WorkCounter);
|
float WorkSecondsElapsed = SDLGetSecondsElapsed(LastCounter, WorkCounter);
|
||||||
|
|
||||||
F32 SecondsElapsedForFrame = WorkSecondsElapsed;
|
float SecondsElapsedForFrame = WorkSecondsElapsed;
|
||||||
if(SecondsElapsedForFrame < TargetSecondsPerFrame) {
|
if(SecondsElapsedForFrame < TargetSecondsPerFrame) {
|
||||||
while(SecondsElapsedForFrame < TargetSecondsPerFrame) {
|
while(SecondsElapsedForFrame < TargetSecondsPerFrame) {
|
||||||
SecondsElapsedForFrame = SDLGetSecondsElapsed(LastCounter, SDLGetWallClock());
|
SecondsElapsedForFrame = SDLGetSecondsElapsed(LastCounter, SDLGetWallClock());
|
||||||
SDL_Delay((U32)((TargetSecondsPerFrame - SecondsElapsedForFrame) * 1000.0f));
|
SDL_Delay((unsigned int)((TargetSecondsPerFrame - SecondsElapsedForFrame) * 1000.0));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: Missed frame rate!
|
// TODO: Missed frame rate!
|
||||||
// TODO: Logging.
|
// TODO: Logging.
|
||||||
}
|
}
|
||||||
|
|
||||||
U64 EndCounter = SDLGetWallClock();
|
unsigned long long EndCounter = SDLGetWallClock();
|
||||||
|
|
||||||
SDLDisplayBufferInWindow(GlobalBackbuffer, Window, Renderer);
|
SDLDisplayBufferInWindow(GlobalBackbuffer, Window, Renderer);
|
||||||
|
|
||||||
F64 MSPerFrame = 1000.0f * SDLGetSecondsElapsed(LastCounter, EndCounter);
|
double MSPerFrame = 1000.0 * SDLGetSecondsElapsed(LastCounter, EndCounter);
|
||||||
F64 FPS = 0.0f;
|
double FPS = 0.0;
|
||||||
|
|
||||||
LastCounter = EndCounter;
|
LastCounter = EndCounter;
|
||||||
|
|
||||||
#if __x86_64__ || __i386__
|
#if __x86_64__ || __i386__
|
||||||
U64 EndCycleCount = __rdtsc();
|
unsigned long long EndCycleCount = __rdtsc();
|
||||||
U64 CyclesElapsed = EndCycleCount - LastCycleCount;
|
unsigned long long CyclesElapsed = EndCycleCount - LastCycleCount;
|
||||||
F64 MCPF = ((F64)CyclesElapsed / (1000.0f * 1000.0f));
|
double MCPF = ((double)CyclesElapsed / (1000.0 * 1000.0));
|
||||||
fprintf(stderr, "%.02f ms/f, %.02f/s, %.02f mc/f\n", MSPerFrame, FPS, MCPF);
|
fprintf(stderr, "%.02f ms/f, %.02f/s, %.02f mc/f\n", MSPerFrame, FPS, MCPF);
|
||||||
LastCycleCount = EndCycleCount;
|
LastCycleCount = EndCycleCount;
|
||||||
#else
|
#else
|
||||||
fprintf(stderr, "%.02f ms/f, %.02f/s\n", MSPerFrame, FPS);
|
fprintf(stderr, "%.02f ms/f, %.02f/s\n", MSPerFrame, FPS);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Swap(game_input *, NewInput, OldInput);
|
SWAP(game_input *, NewInput, OldInput);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: Failed to allocate Samples and Sound memory . . .
|
// TODO: Failed to allocate Samples and Sound memory . . .
|
||||||
|
|||||||
Reference in New Issue
Block a user