Comply with ansi C and clean up.
This commit is contained in:
+247
-154
@@ -49,7 +49,8 @@ static void ClearBackground(struct game_offscreen_buffer *Buffer)
|
||||
int Width = Buffer->Width;
|
||||
int Height = Buffer->Height;
|
||||
|
||||
for(int X = 0; X < Width * Height; X++) {
|
||||
int X;
|
||||
for(X = 0; X < Width * Height; X++) {
|
||||
unsigned int *Pixel = &((unsigned int *)Buffer->Memory)[X];
|
||||
*Pixel = 0xFFFFFF;
|
||||
}
|
||||
@@ -59,13 +60,24 @@ static void DrawRectangle(struct game_offscreen_buffer *Buffer,
|
||||
struct v2 vMin, struct v2 vMax,
|
||||
float R, float G, float B)
|
||||
{
|
||||
int MinX = RoundFloatToInt(vMin.X);
|
||||
int MinY = RoundFloatToInt(vMin.Y);
|
||||
int MaxX = RoundFloatToInt(vMax.X);
|
||||
int MaxY = RoundFloatToInt(vMax.Y);
|
||||
int Y, X;
|
||||
|
||||
int Width = Buffer->Width;
|
||||
int Height = Buffer->Height;
|
||||
int Width, Height;
|
||||
|
||||
int MinX, MinY, MaxX, MaxY;
|
||||
|
||||
unsigned int Color;
|
||||
|
||||
unsigned char *Row;
|
||||
unsigned int *Pixel;
|
||||
|
||||
MinX = RoundFloatToInt(vMin.X);
|
||||
MinY = RoundFloatToInt(vMin.Y);
|
||||
MaxX = RoundFloatToInt(vMax.X);
|
||||
MaxY = RoundFloatToInt(vMax.Y);
|
||||
|
||||
Width = Buffer->Width;
|
||||
Height = Buffer->Height;
|
||||
|
||||
if(MinX < 0)
|
||||
MinX = 0;
|
||||
@@ -80,16 +92,16 @@ static void DrawRectangle(struct game_offscreen_buffer *Buffer,
|
||||
/*unsigned int Color = (unsigned int)((RoundFloatToInt(R * 255.0f) << 16) |
|
||||
(RoundFloatToInt(G * 255.0f) << 8) |
|
||||
(RoundFloatToInt(B * 255.0f) << 0));*/
|
||||
unsigned int Color = (unsigned int)((RoundFloatToInt(255.0f) << 24) |
|
||||
(RoundFloatToInt(R * 255.0f) << 16) |
|
||||
(RoundFloatToInt(G * 255.0f) << 8) |
|
||||
(RoundFloatToInt(B * 255.0f) << 0));
|
||||
Color = (unsigned int)((RoundFloatToInt(255.0f) << 24) |
|
||||
(RoundFloatToInt(R * 255.0f) << 16) |
|
||||
(RoundFloatToInt(G * 255.0f) << 8) |
|
||||
(RoundFloatToInt(B * 255.0f) << 0));
|
||||
|
||||
unsigned char *Row = ((unsigned char *)Buffer->Memory +
|
||||
MinX*Buffer->BytesPerPixel + MinY*Buffer->Pitch);
|
||||
for(int Y = MinY; Y < MaxY; Y++) {
|
||||
unsigned int *Pixel = (unsigned int *)Row;
|
||||
for(int X = MinX; X < MaxX; X++) {
|
||||
Row = ((unsigned char *)Buffer->Memory + MinX*Buffer->BytesPerPixel +
|
||||
MinY*Buffer->Pitch);
|
||||
for(Y = MinY; Y < MaxY; Y++) {
|
||||
Pixel = (unsigned int *)Row;
|
||||
for(X = MinX; X < MaxX; X++) {
|
||||
*(unsigned int *)Pixel = Color;
|
||||
Pixel++;
|
||||
}
|
||||
@@ -101,24 +113,35 @@ static void DrawBitmap(struct game_offscreen_buffer *Buffer,
|
||||
struct loaded_bitmap *Bitmap,
|
||||
float RelX, float RelY, int AlignX, int AlignY)
|
||||
{
|
||||
int MinX, MinY, MaxX, MaxY;
|
||||
|
||||
int Width, Height;
|
||||
|
||||
int SourceOffsetX, SourceOffsetY;
|
||||
|
||||
unsigned int *SourceRow;
|
||||
unsigned char *DestRow;
|
||||
|
||||
int Y, X;
|
||||
|
||||
RelX -= (float)AlignX;
|
||||
RelY -= (float)AlignY;
|
||||
|
||||
int MinX = RoundFloatToInt(RelX);
|
||||
int MinY = RoundFloatToInt(RelY);
|
||||
int MaxX = RoundFloatToInt(RelX + (float)Bitmap->Width);
|
||||
int MaxY = RoundFloatToInt(RelY + (float)Bitmap->Height);
|
||||
MinX = RoundFloatToInt(RelX);
|
||||
MinY = RoundFloatToInt(RelY);
|
||||
MaxX = RoundFloatToInt(RelX + (float)Bitmap->Width);
|
||||
MaxY = RoundFloatToInt(RelY + (float)Bitmap->Height);
|
||||
|
||||
int Width = Buffer->Width;
|
||||
int Height = Buffer->Height;
|
||||
Width = Buffer->Width;
|
||||
Height = Buffer->Height;
|
||||
|
||||
int SourceOffsetX = 0;
|
||||
SourceOffsetX = 0;
|
||||
if(MinX < 0) {
|
||||
SourceOffsetX = -MinX;
|
||||
MinX = 0;
|
||||
}
|
||||
|
||||
int SourceOffsetY = 0;
|
||||
SourceOffsetY = 0;
|
||||
if(MinY < 0) {
|
||||
SourceOffsetY = -MinY;
|
||||
MinY = 0;
|
||||
@@ -129,15 +152,15 @@ static void DrawBitmap(struct game_offscreen_buffer *Buffer,
|
||||
if(MaxY > Height)
|
||||
MaxY = Height;
|
||||
|
||||
unsigned int *SourceRow =
|
||||
SourceRow =
|
||||
Bitmap->Pixels + Bitmap->Width*(Bitmap->Height - 1);
|
||||
SourceRow += -Bitmap->Width*SourceOffsetY + SourceOffsetX;
|
||||
unsigned char *DestRow = ((unsigned char *)Buffer->Memory +
|
||||
DestRow = ((unsigned char *)Buffer->Memory +
|
||||
MinX*Buffer->BytesPerPixel + MinY*Buffer->Pitch);
|
||||
for(int Y = MinY; Y < MaxY; Y++) {
|
||||
for(Y = MinY; Y < MaxY; Y++) {
|
||||
unsigned int *Dest = (unsigned int *)DestRow;
|
||||
unsigned int *Source = SourceRow;
|
||||
for(int X = MinX; X < MaxX; X++) {
|
||||
for(X = MinX; X < MaxX; X++) {
|
||||
float SA = (float)((*Source >> 24) & 0xFF);
|
||||
float SR = (float)((*Source >> 16) & 0xFF);
|
||||
float SG = (float)((*Source >> 8) & 0xFF);
|
||||
@@ -149,7 +172,7 @@ static void DrawBitmap(struct game_offscreen_buffer *Buffer,
|
||||
|
||||
float A = SA / 255.0f;
|
||||
|
||||
// TODO: This should be replaced by premultiplied alpha.
|
||||
/* TODO: This should be replaced by premultiplied alpha. */
|
||||
float R = (1.0f-A)*DR + A*SR;
|
||||
float G = (1.0f-A)*DG + A*SG;
|
||||
float B = (1.0f-A)*DB + A*SB;
|
||||
@@ -198,47 +221,56 @@ static struct loaded_bitmap DEBUGLoadBMP(struct thread_context *Thread,
|
||||
const char *), char *FileName)
|
||||
{
|
||||
struct loaded_bitmap Result;
|
||||
|
||||
struct debug_read_file_result ReadResult;
|
||||
|
||||
memset(&Result, 0, sizeof(Result));
|
||||
|
||||
struct debug_read_file_result ReadResult =
|
||||
DEBUGPlatformReadEntireFile(Thread, FileName);
|
||||
ReadResult = DEBUGPlatformReadEntireFile(Thread, FileName);
|
||||
if(ReadResult.ContentsSize > 0) {
|
||||
struct bitmap_header *Header =
|
||||
(struct bitmap_header *)ReadResult.Contents;
|
||||
struct bitmap_header *Header;
|
||||
|
||||
unsigned int *Pixels =
|
||||
(unsigned int *)((unsigned char *)ReadResult.Contents +
|
||||
Header->BitmapOffset);
|
||||
unsigned int *Pixels;
|
||||
|
||||
unsigned int RedMask, GreenMask, BlueMask, AlphaMask;
|
||||
|
||||
struct bit_scan_result RedShift, GreenShift,
|
||||
BlueShift, AlphaShift;
|
||||
|
||||
unsigned int *SourceDest;
|
||||
|
||||
int X, Y;
|
||||
|
||||
Header = (struct bitmap_header *)ReadResult.Contents;
|
||||
|
||||
Pixels = (unsigned int *)((unsigned char *)ReadResult.Contents +
|
||||
Header->BitmapOffset);
|
||||
Result.Pixels = Pixels;
|
||||
Result.Width = Header->Width;
|
||||
Result.Height = Header->Height;
|
||||
|
||||
ASSERT(Header->Compression == 3);
|
||||
|
||||
// NOTE: The byte order in memory is determined by the Header.
|
||||
/* NOTE: The byte order in memory is determined by the Header. */
|
||||
|
||||
unsigned int RedMask = Header->RedMask;
|
||||
unsigned int GreenMask = Header->GreenMask;
|
||||
unsigned int BlueMask = Header->BlueMask;
|
||||
unsigned int AlphaMask = ~(RedMask | GreenMask | BlueMask);
|
||||
RedMask = Header->RedMask;
|
||||
GreenMask = Header->GreenMask;
|
||||
BlueMask = Header->BlueMask;
|
||||
AlphaMask = ~(RedMask | GreenMask | BlueMask);
|
||||
|
||||
struct bit_scan_result RedShift =
|
||||
FindLeastSignificantSetBit(RedMask);
|
||||
struct bit_scan_result GreenShift =
|
||||
FindLeastSignificantSetBit(GreenMask);
|
||||
struct bit_scan_result BlueShift =
|
||||
FindLeastSignificantSetBit(BlueMask);
|
||||
struct bit_scan_result AlphaShift =
|
||||
FindLeastSignificantSetBit(AlphaMask);
|
||||
RedShift = FindLeastSignificantSetBit(RedMask);
|
||||
GreenShift = FindLeastSignificantSetBit(GreenMask);
|
||||
BlueShift = FindLeastSignificantSetBit(BlueMask);
|
||||
AlphaShift = FindLeastSignificantSetBit(AlphaMask);
|
||||
|
||||
ASSERT(RedShift.Found);
|
||||
ASSERT(GreenShift.Found);
|
||||
ASSERT(BlueShift.Found);
|
||||
ASSERT(AlphaShift.Found);
|
||||
|
||||
unsigned int *SourceDest = Pixels;
|
||||
for(int Y = 0; Y < Header->Height; Y++) {
|
||||
for(int X = 0; X < Header->Width; X++) {
|
||||
SourceDest = Pixels;
|
||||
for(Y = 0; Y < Header->Height; Y++) {
|
||||
for(X = 0; X < Header->Width; X++) {
|
||||
unsigned int C = *SourceDest;
|
||||
|
||||
*SourceDest = ((((C >> AlphaShift.Index) & 0xFF) << 24) |
|
||||
@@ -279,9 +311,10 @@ static void InitializePlayer(struct entity *Entity)
|
||||
static unsigned int AddEntity(struct game_state *GameState)
|
||||
{
|
||||
unsigned int EntityIndex = ++GameState->EntityCount;
|
||||
struct entity *Entity;
|
||||
|
||||
ASSERT(GameState->EntityCount < ARRAY_SIZE(GameState->Entities));
|
||||
struct entity *Entity = &GameState->Entities[EntityIndex];
|
||||
Entity = &GameState->Entities[EntityIndex];
|
||||
memset(Entity, 0, sizeof(struct entity));
|
||||
|
||||
return EntityIndex;
|
||||
@@ -290,38 +323,48 @@ static unsigned int AddEntity(struct game_state *GameState)
|
||||
static void MovePlayer(struct game_state *GameState,
|
||||
struct entity *Entity, float dt, struct v2 ddPlayer)
|
||||
{
|
||||
struct tile_map *TileMap = GameState->World->TileMap;
|
||||
struct tile_map *TileMap;
|
||||
|
||||
float PlayerSpeed;
|
||||
|
||||
struct tile_map_position OldPlayerP, NewPlayerP;
|
||||
struct v2 PlayerDelta;
|
||||
|
||||
TileMap = GameState->World->TileMap;
|
||||
|
||||
if((ddPlayer.X != 0.0f) && (ddPlayer.Y != 0.0f))
|
||||
ddPlayer = V2Multiply(0.7071067811865475244f, ddPlayer);
|
||||
|
||||
float PlayerSpeed = 10.0f;
|
||||
PlayerSpeed = 10.0f;
|
||||
ddPlayer = V2Multiply(PlayerSpeed, ddPlayer);
|
||||
|
||||
ddPlayer = V2Add(ddPlayer, V2Unary(V2Multiply(1.5f, Entity->dP)));
|
||||
|
||||
struct tile_map_position OldPlayerP = Entity->P;
|
||||
struct tile_map_position NewPlayerP = OldPlayerP;
|
||||
struct v2 PlayerDelta = V2Add(V2Multiply(0.5f, V2Multiply(Square(dt),
|
||||
ddPlayer)),
|
||||
V2Multiply(dt, Entity->dP));
|
||||
OldPlayerP = Entity->P;
|
||||
NewPlayerP = OldPlayerP;
|
||||
PlayerDelta = V2Add(V2Multiply(0.5f, V2Multiply(Square(dt), ddPlayer)),
|
||||
V2Multiply(dt, Entity->dP));
|
||||
NewPlayerP.Offset = V2Add(PlayerDelta, NewPlayerP.Offset);
|
||||
Entity->dP = V2Add(V2Multiply(dt, ddPlayer), Entity->dP);
|
||||
NewPlayerP = RecannonicalizePosition(TileMap, NewPlayerP);
|
||||
|
||||
{
|
||||
#if 1
|
||||
struct tile_map_position PlayerLeft;
|
||||
struct tile_map_position PlayerLeft, PlayerRight;
|
||||
|
||||
int Collided;
|
||||
|
||||
struct tile_map_position ColP;
|
||||
|
||||
PlayerLeft = NewPlayerP;
|
||||
PlayerLeft.Offset.X -= 0.5f*Entity->Width;
|
||||
PlayerLeft = RecannonicalizePosition(TileMap, PlayerLeft);
|
||||
|
||||
struct tile_map_position PlayerRight;
|
||||
PlayerRight = NewPlayerP;
|
||||
PlayerRight.Offset.X += 0.5f*Entity->Width;
|
||||
PlayerRight = RecannonicalizePosition(TileMap, PlayerRight);
|
||||
|
||||
int Collided = 0;
|
||||
struct tile_map_position ColP;
|
||||
Collided = 0;
|
||||
memset(&ColP, 0, sizeof(ColP));
|
||||
if(!IsTileMapPointEmpty(TileMap, NewPlayerP)) {
|
||||
ColP = NewPlayerP;
|
||||
@@ -339,16 +382,20 @@ static void MovePlayer(struct game_state *GameState,
|
||||
if(Collided) {
|
||||
struct v2 r = { 0, 0 };
|
||||
if(ColP.AbsTileX < Entity->P.AbsTileX) {
|
||||
r = (struct v2){ 1, 0 };
|
||||
r.X = 1;
|
||||
r.Y = 0;
|
||||
}
|
||||
if(ColP.AbsTileX > Entity->P.AbsTileX) {
|
||||
r = (struct v2){ -1, 0 };
|
||||
r.X = -1;
|
||||
r.Y = 0;
|
||||
}
|
||||
if(ColP.AbsTileY < Entity->P.AbsTileY) {
|
||||
r = (struct v2){ 0, 1 };
|
||||
r.X = 0;
|
||||
r.Y = 1;
|
||||
}
|
||||
if(ColP.AbsTileY > Entity->P.AbsTileY) {
|
||||
r = (struct v2){ 0, -1 };
|
||||
r.X = 0;
|
||||
r.Y = -1;
|
||||
}
|
||||
|
||||
Entity->dP = V2Subtract(Entity->dP,
|
||||
@@ -393,6 +440,7 @@ static void MovePlayer(struct game_state *GameState,
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if(!AreOnSameTile(&OldPlayerP, &Entity->P)) {
|
||||
unsigned int NewTileValue = GetTileValueByPos(TileMap, Entity->P);
|
||||
@@ -424,16 +472,49 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
struct game_offscreen_buffer *Buffer,
|
||||
struct game_sound_output_buffer *SoundBuffer)
|
||||
{
|
||||
ASSERT((&Input->Controllers[0].Terminator -
|
||||
&Input->Controllers[0].Buttons[0]) ==
|
||||
ARRAY_SIZE(Input->Controllers[0].Buttons));
|
||||
ASSERT(sizeof(struct game_state) <= Memory->PermanentStorageSize);
|
||||
|
||||
struct game_state *GameState;
|
||||
|
||||
struct world *World;
|
||||
struct tile_map *TileMap;
|
||||
|
||||
int TileSideInPixels;
|
||||
float MetersToPixels;
|
||||
|
||||
unsigned int ControllerIndex;
|
||||
|
||||
struct entity *CameraFollowingEntity;
|
||||
|
||||
float ScreenCenterX, ScreenCenterY;
|
||||
|
||||
int RelRow, RelColumn;
|
||||
|
||||
struct entity *Entity;
|
||||
|
||||
unsigned int EntityIndex;
|
||||
|
||||
ASSERT((&Input->Controllers[0].u.s.Terminator -
|
||||
&Input->Controllers[0].u.Buttons[0]) ==
|
||||
ARRAY_SIZE(Input->Controllers[0].u.Buttons));
|
||||
ASSERT(sizeof(struct game_state) <= Memory->PermanentStorageSize);
|
||||
|
||||
GameState = (struct game_state *)Memory->PermanentStorage;
|
||||
if(!Memory->IsInitialized) {
|
||||
// NOTE: Reserve entity slot 0 for null entity
|
||||
struct hero_bitmaps *Bitmap;
|
||||
struct world *World;
|
||||
struct tile_map *TileMap;
|
||||
|
||||
unsigned int RandomNumberIndex;
|
||||
|
||||
unsigned int TilesPerWidth, TilesPerHeight;
|
||||
unsigned int ScreenX, ScreenY;
|
||||
|
||||
unsigned int AbsTileZ;
|
||||
|
||||
int DoorTop, DoorBottom, DoorLeft, DoorRight, DoorUp, DoorDown;
|
||||
|
||||
unsigned int ScreenIndex;
|
||||
|
||||
/* NOTE: Reserve entity slot 0 for null entity */
|
||||
AddEntity(GameState);
|
||||
|
||||
GameState->rect_pos.X = 0.0f;
|
||||
@@ -464,8 +545,6 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
GameState->Backdrop = DEBUGLoadBMP(Thread,
|
||||
Memory->DEBUGPlatformReadEntireFile, "data/test_background.bmp");
|
||||
|
||||
struct hero_bitmaps *Bitmap;
|
||||
|
||||
Bitmap = &GameState->HeroBitmaps[0];
|
||||
Bitmap->AlignX = 72;
|
||||
Bitmap->AlignY = 182;
|
||||
@@ -527,10 +606,10 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
sizeof(struct game_state));
|
||||
|
||||
GameState->World = PUSH_STRUCT(&GameState->WorldArena, struct world);
|
||||
struct world *World = GameState->World;
|
||||
World = GameState->World;
|
||||
World->TileMap = PUSH_STRUCT(&GameState->WorldArena, struct tile_map);
|
||||
|
||||
struct tile_map *TileMap = World->TileMap;
|
||||
TileMap = World->TileMap;
|
||||
|
||||
TileMap->ChunkShift = 4;
|
||||
TileMap->ChunkMask = (1 << TileMap->ChunkShift) - 1;
|
||||
@@ -547,33 +626,39 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
|
||||
TileMap->TileSideInMeters = 1.4f;
|
||||
|
||||
unsigned int RandomNumberIndex = 0;
|
||||
RandomNumberIndex = 0;
|
||||
|
||||
unsigned int TilesPerWidth = 17;
|
||||
unsigned int TilesPerHeight = 9;
|
||||
unsigned int ScreenX = 0;
|
||||
unsigned int ScreenY = 0;
|
||||
TilesPerWidth = 17;
|
||||
TilesPerHeight = 9;
|
||||
ScreenX = 0;
|
||||
ScreenY = 0;
|
||||
|
||||
unsigned int AbsTileZ = 0;
|
||||
AbsTileZ = 0;
|
||||
|
||||
int DoorTop = 0;
|
||||
int DoorBottom = 0;
|
||||
int DoorLeft = 0;
|
||||
int DoorRight = 0;
|
||||
int DoorUp = 0;
|
||||
int DoorDown = 0;
|
||||
for(unsigned int ScreenIndex = 0; ScreenIndex < 100; ScreenIndex++) {
|
||||
ASSERT(RandomNumberIndex < ARRAY_SIZE(RandomNumberTable));
|
||||
DoorTop = 0;
|
||||
DoorBottom = 0;
|
||||
DoorLeft = 0;
|
||||
DoorRight = 0;
|
||||
DoorUp = 0;
|
||||
DoorDown = 0;
|
||||
for(ScreenIndex = 0; ScreenIndex < 100; ScreenIndex++) {
|
||||
|
||||
/* TODO: Random number generator. */
|
||||
unsigned int RandomChoice;
|
||||
|
||||
int CreatedZDoor;
|
||||
|
||||
unsigned int TileY, TileX;
|
||||
|
||||
ASSERT(RandomNumberIndex < ARRAY_SIZE(RandomNumberTable));
|
||||
|
||||
if(DoorUp || DoorDown) {
|
||||
RandomChoice = RandomNumberTable[RandomNumberIndex++] % 2;
|
||||
} else {
|
||||
RandomChoice = RandomNumberTable[RandomNumberIndex++] % 3;
|
||||
}
|
||||
|
||||
int CreatedZDoor = 0;
|
||||
CreatedZDoor = 0;
|
||||
|
||||
if(RandomChoice == 2) {
|
||||
CreatedZDoor = 1;
|
||||
@@ -589,14 +674,8 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
DoorTop = 1;
|
||||
}
|
||||
|
||||
for(unsigned int TileY = 0;
|
||||
TileY < TilesPerHeight;
|
||||
TileY++)
|
||||
{
|
||||
for(unsigned int TileX = 0;
|
||||
TileX < TilesPerWidth;
|
||||
TileX++)
|
||||
{
|
||||
for(TileY = 0; TileY < TilesPerHeight; TileY++) {
|
||||
for(TileX = 0; TileX < TilesPerWidth; TileX++) {
|
||||
unsigned int AbsTileX = ScreenX * TilesPerWidth + TileX;
|
||||
unsigned int AbsTileY = ScreenY * TilesPerHeight + TileY;
|
||||
|
||||
@@ -669,58 +748,56 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
Memory->IsInitialized = 1;
|
||||
}
|
||||
|
||||
struct world *World = GameState->World;
|
||||
struct tile_map *TileMap = World->TileMap;
|
||||
World = GameState->World;
|
||||
TileMap = World->TileMap;
|
||||
|
||||
int TileSideInPixels = 60;
|
||||
float MetersToPixels =
|
||||
(float)TileSideInPixels / TileMap->TileSideInMeters;
|
||||
TileSideInPixels = 60;
|
||||
MetersToPixels = (float)TileSideInPixels / TileMap->TileSideInMeters;
|
||||
|
||||
for(unsigned int ControllerIndex = 0;
|
||||
for(ControllerIndex = 0;
|
||||
ControllerIndex < ARRAY_SIZE(Input->Controllers);
|
||||
ControllerIndex++)
|
||||
{
|
||||
struct game_controller_input *Controller =
|
||||
GetController(Input, ControllerIndex);
|
||||
struct entity *ControllingEntity =
|
||||
GetEntity(GameState,
|
||||
GameState->PlayerIndexForController[ControllerIndex]);
|
||||
struct game_controller_input *Controller;
|
||||
struct entity *ControllingEntity;
|
||||
|
||||
Controller = GetController(Input, ControllerIndex);
|
||||
ControllingEntity = GetEntity(GameState,
|
||||
GameState->PlayerIndexForController[ControllerIndex]);
|
||||
if(ControllingEntity) {
|
||||
struct v2 ddPlayer; /* accerlation */
|
||||
memset(&ddPlayer, 0, sizeof(ddPlayer));
|
||||
|
||||
if(Controller->IsAnalog) {
|
||||
ddPlayer = (struct v2){ Controller->StickAverageX,
|
||||
Controller->StickAverageY };
|
||||
ddPlayer.X = Controller->StickAverageX;
|
||||
ddPlayer.Y = -Controller->StickAverageY;
|
||||
} else {
|
||||
|
||||
if(Controller->MoveUp.EndedDown) {
|
||||
if(Controller->u.s.MoveUp.EndedDown) {
|
||||
GameState->rect_pos.Y -= 1.0f;
|
||||
|
||||
ddPlayer.Y = 1.0f;
|
||||
}
|
||||
if(Controller->MoveDown.EndedDown) {
|
||||
if(Controller->u.s.MoveDown.EndedDown) {
|
||||
GameState->rect_pos.Y += 1.0f;
|
||||
|
||||
ddPlayer.Y = -1.0f;
|
||||
}
|
||||
if(Controller->MoveLeft.EndedDown) {
|
||||
if(Controller->u.s.MoveLeft.EndedDown) {
|
||||
GameState->rect_pos.X -= 1.0f;
|
||||
|
||||
ddPlayer.X = -1.0f;
|
||||
}
|
||||
if(Controller->MoveRight.EndedDown) {
|
||||
if(Controller->u.s.MoveRight.EndedDown) {
|
||||
GameState->rect_pos.X += 1.0f;
|
||||
|
||||
ddPlayer.X = 1.0f;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
MovePlayer(GameState, ControllingEntity, Input->dtForFrame,
|
||||
ddPlayer);
|
||||
} else {
|
||||
if(Controller->Start.EndedDown) {
|
||||
if(Controller->u.s.Start.EndedDown) {
|
||||
unsigned int EntityIndex = AddEntity(GameState);
|
||||
ControllingEntity = GetEntity(GameState, EntityIndex);
|
||||
InitializePlayer(ControllingEntity);
|
||||
@@ -730,14 +807,15 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
}
|
||||
}
|
||||
|
||||
struct entity *CameraFollowingEntity =
|
||||
CameraFollowingEntity =
|
||||
GetEntity(GameState, GameState->CameraFollowingEntityIndex);
|
||||
if(CameraFollowingEntity) {
|
||||
struct tile_map_difference Diff;
|
||||
|
||||
GameState->CameraP.AbsTileZ = CameraFollowingEntity->P.AbsTileZ;
|
||||
|
||||
struct tile_map_difference Diff =
|
||||
SubtractInFloat(TileMap, &CameraFollowingEntity->P,
|
||||
&GameState->CameraP);
|
||||
Diff = SubtractInFloat(TileMap, &CameraFollowingEntity->P,
|
||||
&GameState->CameraP);
|
||||
if(Diff.dXY.X > (9.0f*TileMap->TileSideInMeters)) {
|
||||
GameState->CameraP.AbsTileX += 17;
|
||||
}
|
||||
@@ -758,11 +836,11 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
|
||||
DrawBitmap(Buffer, &GameState->Backdrop, 0.0f, 0.0f, 0, 0);
|
||||
|
||||
float ScreenCenterX = 0.5f * (float)Buffer->Width;
|
||||
float ScreenCenterY = 0.5f * (float)Buffer->Height;
|
||||
ScreenCenterX = 0.5f * (float)Buffer->Width;
|
||||
ScreenCenterY = 0.5f * (float)Buffer->Height;
|
||||
|
||||
for(int RelRow = -10; RelRow < 10; RelRow++) {
|
||||
for(int RelColumn = -20; RelColumn < 20; RelColumn++) {
|
||||
for(RelRow = -10; RelRow < 10; RelRow++) {
|
||||
for(RelColumn = -20; RelColumn < 20; RelColumn++) {
|
||||
unsigned int Column = GameState->CameraP.AbsTileX + RelColumn;
|
||||
unsigned int Row = GameState->CameraP.AbsTileY + RelRow;
|
||||
|
||||
@@ -770,7 +848,12 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
GameState->CameraP.AbsTileZ);
|
||||
|
||||
if(TileID > 1) {
|
||||
struct v2 TileSide, Center;
|
||||
|
||||
struct v2 Min, Max;
|
||||
|
||||
float Gray = 0.5f;
|
||||
|
||||
if(TileID == 2) {
|
||||
Gray = 1.0f;
|
||||
}
|
||||
@@ -785,48 +868,58 @@ void UpdateAndRender(struct thread_context *Thread,
|
||||
Gray = 0.0f;
|
||||
}
|
||||
|
||||
struct v2 TileSide = { 0.5f*(float)TileSideInPixels,
|
||||
0.5f*(float)TileSideInPixels };
|
||||
struct v2 Center = { ScreenCenterX -
|
||||
TileSide.X = 0.5f*(float)TileSideInPixels;
|
||||
TileSide.Y = 0.5f*(float)TileSideInPixels;
|
||||
Center.X = ScreenCenterX -
|
||||
MetersToPixels*GameState->CameraP.Offset.X +
|
||||
(float)RelColumn*(float)TileSideInPixels,
|
||||
ScreenCenterY + MetersToPixels *
|
||||
(float)RelColumn*(float)TileSideInPixels;
|
||||
Center.Y = ScreenCenterY + MetersToPixels *
|
||||
GameState->CameraP.Offset.Y -
|
||||
(float)RelRow*(float)TileSideInPixels };
|
||||
struct v2 Min = V2Subtract(Center, TileSide);
|
||||
struct v2 Max = V2Add(Center, TileSide);
|
||||
(float)RelRow*(float)TileSideInPixels;
|
||||
Min = V2Subtract(Center, TileSide);
|
||||
Max = V2Add(Center, TileSide);
|
||||
DrawRectangle(Buffer, Min, Max, Gray, Gray, Gray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct entity *Entity = GameState->Entities;
|
||||
for(unsigned int EntityIndex = 0;
|
||||
Entity = GameState->Entities;
|
||||
|
||||
for(EntityIndex = 0;
|
||||
EntityIndex < GameState->EntityCount;
|
||||
EntityIndex++, Entity++)
|
||||
{
|
||||
if(Entity->Exists) {
|
||||
struct tile_map_difference Diff =
|
||||
SubtractInFloat(TileMap, &Entity->P, &GameState->CameraP);
|
||||
struct tile_map_difference Diff;
|
||||
|
||||
float PlayerR, PlayerG, PlayerB;
|
||||
float PlayerGroundPointX, PlayerGroundPointY;
|
||||
struct v2 PlayerLeftTop, PlayerWidthHeight;
|
||||
|
||||
struct hero_bitmaps *Bitmap;
|
||||
|
||||
Diff = SubtractInFloat(TileMap, &Entity->P, &GameState->CameraP);
|
||||
|
||||
PlayerR = 1.0f;
|
||||
PlayerG = 0.0f;
|
||||
PlayerB = 0.0f;
|
||||
PlayerGroundPointX = ScreenCenterX + MetersToPixels*Diff.dXY.X;
|
||||
PlayerGroundPointY = ScreenCenterY - MetersToPixels*Diff.dXY.Y;
|
||||
|
||||
PlayerLeftTop.X =
|
||||
PlayerGroundPointX - 0.5f * MetersToPixels*Entity->Width;
|
||||
PlayerLeftTop.Y =
|
||||
PlayerGroundPointY - MetersToPixels*Entity->Height;
|
||||
|
||||
PlayerWidthHeight.X = Entity->Width;
|
||||
PlayerWidthHeight.X = Entity->Height;
|
||||
|
||||
float PlayerR = 1.0f;
|
||||
float PlayerG = 0.0f;
|
||||
float PlayerB = 0.0f;
|
||||
float PlayerGroundPointX =
|
||||
ScreenCenterX + MetersToPixels*Diff.dXY.X;
|
||||
float PlayerGroundPointY =
|
||||
ScreenCenterY - MetersToPixels*Diff.dXY.Y;
|
||||
struct v2 PlayerLeftTop =
|
||||
{ PlayerGroundPointX - 0.5f * MetersToPixels*Entity->Width,
|
||||
PlayerGroundPointY - MetersToPixels*Entity->Height };
|
||||
struct v2 PlayerWidthHeight = { Entity->Width, Entity->Height };
|
||||
DrawRectangle(Buffer, PlayerLeftTop,
|
||||
V2Add(PlayerLeftTop, V2Multiply(MetersToPixels,
|
||||
PlayerWidthHeight)),
|
||||
PlayerR, PlayerG, PlayerB);
|
||||
|
||||
struct hero_bitmaps *Bitmap =
|
||||
&GameState->HeroBitmaps[Entity->FacingDirection];
|
||||
Bitmap = &GameState->HeroBitmaps[Entity->FacingDirection];
|
||||
DrawBitmap(Buffer, &Bitmap->Torso,
|
||||
PlayerGroundPointX, PlayerGroundPointY,
|
||||
Bitmap->AlignX, Bitmap->AlignY);
|
||||
|
||||
Reference in New Issue
Block a user