188 lines
4.7 KiB
C
188 lines
4.7 KiB
C
#include "core.h"
|
|
#include "handmade.h"
|
|
#include "sdl_handmade.h"
|
|
|
|
#include <math.h>
|
|
|
|
static void GameOutputSound(game_state *GameState,
|
|
game_sound_output_buffer *SoundBuffer,
|
|
S32 ToneHz)
|
|
{
|
|
/*
|
|
S16 ToneVolume, *SampleOut;
|
|
S32 WavePeriod, SampleIndex;
|
|
|
|
ToneVolume = 0;
|
|
WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz;
|
|
|
|
SampleOut = SoundBuffer->Samples;
|
|
|
|
for(SampleIndex = 0;
|
|
SampleIndex < SoundBuffer->SampleCount;
|
|
SampleIndex++)
|
|
{
|
|
F32 SineValue;
|
|
S16 SampleValue;
|
|
|
|
SineValue = sinf(GameState->tSine);
|
|
SampleValue = (S16)(SineValue * ToneVolume);
|
|
*SampleOut++ = SampleValue;
|
|
*SampleOut++ = SampleValue;
|
|
|
|
GameState->tSine += 2.0f * (F32)PI * 1.0f / (F32)WavePeriod;
|
|
if(GameState->tSine > 2.0f*(F32)PI) {
|
|
GameState->tSine -= 2.0f*(F32)PI;
|
|
}
|
|
}
|
|
*/
|
|
}
|
|
|
|
static void ClearBackground(game_offscreen_buffer *Buffer)
|
|
{
|
|
S32 Width, Height, X;
|
|
|
|
Width = Buffer->Width;
|
|
Height = Buffer->Height;
|
|
|
|
for(X = 0; X < Width * Height; X++) {
|
|
U32 *Pixel = &((U32 *)Buffer->Memory)[X];
|
|
*Pixel = 0xFFFFFF;
|
|
}
|
|
}
|
|
|
|
static S32 RoundF32toS32(F32 Real32)
|
|
{
|
|
S32 Result = (S32)(Real32 + 0.5f);
|
|
return Result;
|
|
}
|
|
|
|
static void DrawRectangle(game_offscreen_buffer *Buffer,
|
|
F32 RealMinX, F32 RealMinY,
|
|
F32 RealMaxX, F32 RealMaxY,
|
|
U32 Color)
|
|
/*F32 R, F32 G, F32 B)*/
|
|
{
|
|
S32 MinX = RoundF32toS32(RealMinX);
|
|
S32 MinY = RoundF32toS32(RealMinY);
|
|
S32 MaxX = RoundF32toS32(RealMaxX);
|
|
S32 MaxY = RoundF32toS32(RealMaxY);
|
|
|
|
S32 Width = Buffer->Width;
|
|
S32 Height = Buffer->Height;
|
|
|
|
U8 *Row;
|
|
S32 Y, X;
|
|
|
|
if(MinX < 0)
|
|
MinX = 0;
|
|
if(MinY < 0)
|
|
MinY = 0;
|
|
|
|
if(MaxX > Width)
|
|
MaxX = Width;
|
|
if(MaxY > Height)
|
|
MaxY = Height;
|
|
|
|
Row = ((U8 *)Buffer->Memory + MinX*Buffer->BytesPerPixel +
|
|
MinY*Buffer->Pitch);
|
|
for(Y = MinY; Y < MaxY; Y++) {
|
|
U32 *Pixel = (U32 *)Row;
|
|
for(X = MinX; X < MaxX; X++) {
|
|
*(U32 *)Pixel = Color;
|
|
Pixel++;
|
|
}
|
|
Row += Buffer->Pitch;
|
|
}
|
|
}
|
|
|
|
static void RenderRectangle(game_offscreen_buffer *Buffer, S32 MouseX,
|
|
S32 MouseY)
|
|
{
|
|
U8 *Row;
|
|
U32 *Pixel;
|
|
S32 X, Y;
|
|
|
|
for(Y = MouseY; Y < MouseY+10; Y++) {
|
|
if(Y >= Buffer->Height)
|
|
break;
|
|
|
|
Row = (U8 *)Buffer->Memory + (Buffer->Pitch * Y);
|
|
for(X = MouseX; X < MouseX+10; X++) {
|
|
if(X >= Buffer->Width)
|
|
break;
|
|
|
|
Pixel = (U32 *)Row + X;
|
|
*Pixel = 0xFFFFFF;
|
|
}
|
|
}
|
|
}
|
|
|
|
void UpdateAndRender(thread_context *Thread, game_memory *Memory,
|
|
game_input *Input, game_offscreen_buffer *Buffer,
|
|
game_sound_output_buffer *SoundBuffer)
|
|
{
|
|
game_state *GameState;
|
|
U32 ControllerIndex;
|
|
|
|
ASSERT((&Input->Controllers[0].u.buttons.Terminator -
|
|
&Input->Controllers[0].u.Buttons[0]) ==
|
|
ARRAY_SIZE(Input->Controllers[0].u.Buttons));
|
|
ASSERT(sizeof(game_state) <= Memory->PermanentStorageSize);
|
|
|
|
GameState = (game_state *)Memory->PermanentStorage;
|
|
if(!Memory->IsInitialized) {
|
|
Memory->IsInitialized = TRUE;
|
|
}
|
|
|
|
for(ControllerIndex = 0;
|
|
ControllerIndex < ARRAY_SIZE(Input->Controllers);
|
|
ControllerIndex++)
|
|
{
|
|
game_controller_input *Controller =
|
|
GetController(Input, ControllerIndex);
|
|
if(Controller->IsAnalog) {
|
|
} else {
|
|
}
|
|
}
|
|
|
|
DrawRectangle(Buffer, 0.0f, 0.0f, (F32)(Buffer->Width),
|
|
(F32)(Buffer->Height), 0x000000);
|
|
DrawRectangle(Buffer, 10.0f, 10.0f, 20.0f, 20.0f, 0xFFFFFF);
|
|
|
|
DrawRectangle(Buffer,
|
|
(F32)(Input->MouseX), (F32)(Input->MouseY),
|
|
(F32)(Input->MouseX) + 10.0f, (F32)(Input->MouseY) + 10.0f,
|
|
0xFF0000);
|
|
GameOutputSound(GameState, SoundBuffer, 400);
|
|
}
|
|
|
|
/*
|
|
static void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset,
|
|
S32 YOffset)
|
|
{
|
|
S32 Width, Height, Y;
|
|
U8 *Row;
|
|
|
|
Width = Buffer->Width;
|
|
Height = Buffer->Height;
|
|
|
|
Row = (U8 *)Buffer->Memory;
|
|
for(Y = 0; Y < Height; Y++) {
|
|
U32 *Pixel = (U32 *)Row;
|
|
S32 X;
|
|
|
|
Pixel = (U32 *)Row;
|
|
for(X = 0; X < Width; X++) {
|
|
U8 Blue, Green;
|
|
|
|
Blue = (U8)(X + XOffset);
|
|
Green = (U8)(Y + YOffset);
|
|
|
|
*Pixel = (Green << 8) | Blue;
|
|
Pixel++;
|
|
}
|
|
Row += Buffer->Pitch;
|
|
}
|
|
}
|
|
*/
|