update
This commit is contained in:
@@ -1,2 +0,0 @@
|
|||||||
call build || exit /b %errorlevel%
|
|
||||||
devenv .\build\handmade_win32.exe
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
call build || exit /b %errorlevel%
|
|
||||||
.\build\handmade_win32.exe
|
|
||||||
1
misc/cmds/run.bat
Normal file
1
misc/cmds/run.bat
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.\build\handmade_win32.exe
|
||||||
2
misc/hh.bat
Normal file
2
misc/hh.bat
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
call .\misc\cmds\%1.bat || exit /b %errorlevel%
|
||||||
|
.\build\handmade_win32.exe
|
||||||
136
src/handmade.cpp
136
src/handmade.cpp
@@ -1,23 +1,5 @@
|
|||||||
#include "handmade.h"
|
#include "handmade.h"
|
||||||
|
#include "handmade_intrinsics.h"
|
||||||
#define PI32 3.141592653589f
|
|
||||||
|
|
||||||
inline int32 roundReal32ToUInt32(real32 realNum) {
|
|
||||||
return (uint32)(realNum + 0.5f);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int32 roundReal32ToInt32(real32 realNum) {
|
|
||||||
return (int32)(realNum + 0.5f);
|
|
||||||
}
|
|
||||||
|
|
||||||
#include "math.h"
|
|
||||||
inline int32 floorReal32ToInt32(real32 realNum) {
|
|
||||||
return (int32)floorf(realNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int32 truncateReal32ToInt32(real32 realNum) {
|
|
||||||
return (int32)realNum;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void drawRectangle(GameOffscreenBuffer *buffer, real32 realMinX, real32 realMinY, real32 realMaxX, real32 realMaxY, real32 R, real32 G, real32 B) {
|
internal void drawRectangle(GameOffscreenBuffer *buffer, real32 realMinX, real32 realMinY, real32 realMaxX, real32 realMaxY, real32 R, real32 G, real32 B) {
|
||||||
int32 minX = roundReal32ToInt32(realMinX);
|
int32 minX = roundReal32ToInt32(realMinX);
|
||||||
@@ -87,51 +69,39 @@ inline TileMap *getTileMap(World *world, int32 tileMapX, int32 tileMapY) {
|
|||||||
return tileMap;
|
return tileMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline CanonicalPosition getCanonicalPosition(World* world, RawPosition pos) {
|
inline void recanonicaliseOrd(World *world, int32 tileCount, int32 *tileMapOrd, int32 *tileOrd, real32* ord) {
|
||||||
CanonicalPosition result = {};
|
int32 offset = floorReal32ToInt32(*ord / world->tileSideInPixels);
|
||||||
|
*tileOrd += offset;
|
||||||
|
*ord -= offset*world->tileSideInPixels;
|
||||||
|
|
||||||
result.tileMapX = pos.tileMapX;
|
Assert(*ord >= 0);
|
||||||
result.tileMapY = pos.tileMapY;
|
Assert(*ord < world->tileSideInPixels);
|
||||||
|
|
||||||
real32 x = pos.x - world->upperLeftX;
|
if (*tileOrd < 0) {
|
||||||
real32 y = pos.y - world->upperLeftY;
|
*tileOrd += tileCount;
|
||||||
|
*tileMapOrd -= 1;
|
||||||
result.tileX = floorReal32ToInt32(x / world->tileWidth);
|
|
||||||
result.tileY = floorReal32ToInt32(y / world->tileHeight);
|
|
||||||
|
|
||||||
result.x = pos.x - result.tileX*world->tileWidth;
|
|
||||||
result.y = pos.y - result.tileY*world->tileHeight;
|
|
||||||
|
|
||||||
if (result.tileX < 0) {
|
|
||||||
result.tileX += world->tileCountX;
|
|
||||||
result.tileMapX -= 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.tileY < 0) {
|
if (*tileOrd >= tileCount) {
|
||||||
result.tileY += world->tileCountY;
|
*tileOrd -= tileCount;
|
||||||
result.tileMapY -= 1;
|
*tileMapOrd += 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (result.tileX >= world->tileCountX) {
|
inline CanonicalPosition recanonicalisePosition(World* world, CanonicalPosition pos) {
|
||||||
result.tileX -= world->tileCountX;
|
CanonicalPosition result = pos;
|
||||||
result.tileMapX += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.tileY >= world->tileCountY) {
|
recanonicaliseOrd(world, world->tileCountX, &result.tileMapX, &result.tileX, &result.x);
|
||||||
result.tileY -= world->tileCountY;
|
recanonicaliseOrd(world, world->tileCountY, &result.tileMapY, &result.tileY, &result.y);
|
||||||
result.tileMapY += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool32 isWorldPointEmpty(World *world, RawPosition testPos) {
|
internal bool32 isWorldPointEmpty(World *world, CanonicalPosition testPos) {
|
||||||
bool32 isEmpty = false;
|
bool32 isEmpty = false;
|
||||||
|
|
||||||
CanonicalPosition canPos = getCanonicalPosition(world, testPos);
|
TileMap *tileMap = getTileMap(world, testPos.tileMapX, testPos.tileMapY);
|
||||||
TileMap *tileMap = getTileMap(world, canPos.tileMapX, canPos.tileMapY);
|
isEmpty = (getTileValueUnchecked(world, tileMap, testPos.tileX, testPos.tileY) == 0);
|
||||||
|
|
||||||
isEmpty = (getTileValueUnchecked(world, tileMap, canPos.tileX, canPos.tileY) == 0);
|
|
||||||
|
|
||||||
return isEmpty;
|
return isEmpty;
|
||||||
}
|
}
|
||||||
@@ -142,11 +112,12 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) {
|
|||||||
GameState *state = (GameState*)memory->permanentStorage;
|
GameState *state = (GameState*)memory->permanentStorage;
|
||||||
|
|
||||||
if (!memory->isInitialised) {
|
if (!memory->isInitialised) {
|
||||||
|
state->playerPos.x = 150.0f;
|
||||||
|
state->playerPos.y = 150.0f;
|
||||||
|
state->playerPos.tileMapX = 0;
|
||||||
|
state->playerPos.tileMapY = 0;
|
||||||
|
|
||||||
memory->isInitialised = true;
|
memory->isInitialised = true;
|
||||||
state->playerX = 150.0f;
|
|
||||||
state->playerY = 150.0f;
|
|
||||||
state->tileMapX = 0;
|
|
||||||
state->tileMapY = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const int TILEMAP_WIDTH = 16;
|
const int TILEMAP_WIDTH = 16;
|
||||||
@@ -210,8 +181,8 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) {
|
|||||||
world.tileMapCountY = WORLD_HEIGHT;
|
world.tileMapCountY = WORLD_HEIGHT;
|
||||||
world.upperLeftX = 0;
|
world.upperLeftX = 0;
|
||||||
world.upperLeftY = 0;
|
world.upperLeftY = 0;
|
||||||
world.tileWidth = 60;
|
world.tileSideInMeters = 1.4f;
|
||||||
world.tileHeight = 60;
|
world.tileSideInPixels = 60;
|
||||||
|
|
||||||
TileMap maps[WORLD_HEIGHT][WORLD_WIDTH];
|
TileMap maps[WORLD_HEIGHT][WORLD_WIDTH];
|
||||||
maps[0][0].tiles = (uint32*)tiles00;
|
maps[0][0].tiles = (uint32*)tiles00;
|
||||||
@@ -256,29 +227,28 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) {
|
|||||||
dPlayerX *= 96.0f;
|
dPlayerX *= 96.0f;
|
||||||
dPlayerY *= 96.0f;
|
dPlayerY *= 96.0f;
|
||||||
|
|
||||||
real32 newPlayerX = state->playerX + dPlayerX * input->dtForFrame;
|
CanonicalPosition newPlayerPos = state->playerPos;
|
||||||
real32 newPlayerY = state->playerY + dPlayerY * input->dtForFrame;
|
newPlayerPos.x += dPlayerX * input->dtForFrame;
|
||||||
|
newPlayerPos.y += dPlayerY * input->dtForFrame;
|
||||||
|
newPlayerPos = recanonicalisePosition(&world, newPlayerPos);
|
||||||
|
|
||||||
RawPosition playerPos = {
|
CanonicalPosition playerBottomLeft = newPlayerPos;
|
||||||
state->tileMapX,
|
|
||||||
state->tileMapY,
|
|
||||||
newPlayerX,
|
|
||||||
newPlayerY,
|
|
||||||
};
|
|
||||||
|
|
||||||
RawPosition playerBottomLeft = playerPos;
|
|
||||||
playerBottomLeft.x -= 0.5f * playerWidth;
|
playerBottomLeft.x -= 0.5f * playerWidth;
|
||||||
|
playerBottomLeft = recanonicalisePosition(&world, playerBottomLeft);
|
||||||
|
|
||||||
RawPosition playerTopLeft = playerPos;
|
CanonicalPosition playerTopLeft = newPlayerPos;
|
||||||
playerTopLeft.x -= 0.5f * playerWidth;
|
playerTopLeft.x -= 0.5f * playerWidth;
|
||||||
playerTopLeft.y -= playerWidth;
|
playerTopLeft.y -= playerWidth;
|
||||||
|
playerTopLeft = recanonicalisePosition(&world, playerTopLeft);
|
||||||
|
|
||||||
RawPosition playerBottomRight = playerPos;
|
CanonicalPosition playerBottomRight = newPlayerPos;
|
||||||
playerBottomRight.x += 0.5f * playerWidth;
|
playerBottomRight.x += 0.5f * playerWidth;
|
||||||
|
playerBottomRight = recanonicalisePosition(&world, playerBottomRight);
|
||||||
|
|
||||||
RawPosition playerTopRight = playerPos;
|
CanonicalPosition playerTopRight = newPlayerPos;
|
||||||
playerTopRight.x += 0.5f * playerWidth;
|
playerTopRight.x += 0.5f * playerWidth;
|
||||||
playerTopRight.y -= playerWidth;
|
playerTopRight.y -= playerWidth;
|
||||||
|
playerTopRight = recanonicalisePosition(&world, playerTopRight);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
isWorldPointEmpty(&world, playerTopLeft) &&
|
isWorldPointEmpty(&world, playerTopLeft) &&
|
||||||
@@ -286,34 +256,30 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) {
|
|||||||
isWorldPointEmpty(&world, playerBottomLeft) &&
|
isWorldPointEmpty(&world, playerBottomLeft) &&
|
||||||
isWorldPointEmpty(&world, playerBottomRight)
|
isWorldPointEmpty(&world, playerBottomRight)
|
||||||
) {
|
) {
|
||||||
CanonicalPosition canPos = getCanonicalPosition(&world, playerPos);
|
state->playerPos = newPlayerPos;
|
||||||
|
|
||||||
state->tileMapX = canPos.tileMapX;
|
|
||||||
state->tileMapY = canPos.tileMapY;
|
|
||||||
|
|
||||||
state->playerX = world.upperLeftX + world.tileWidth*canPos.tileX + canPos.x;
|
|
||||||
state->playerY = world.upperLeftY + world.tileHeight*canPos.tileY + canPos.y;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// clearscreen
|
// clearscreen
|
||||||
drawRectangle(videoBuf, 0.0f, 0.0f, (real32)videoBuf->width, (real32)videoBuf->height, 1.0f, 0.0f, 1.0f);
|
drawRectangle(videoBuf, 0.0f, 0.0f, (real32)videoBuf->width, (real32)videoBuf->height, 1.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
TileMap *currMap = getTileMap(&world, state->tileMapX, state->tileMapY);
|
TileMap *currMap = getTileMap(&world, state->playerPos.tileMapX, state->playerPos.tileMapY);
|
||||||
for (int row = 0; row < world.tileCountY; row++) {
|
for (int row = 0; row < world.tileCountY; row++) {
|
||||||
for (int col = 0; col < world.tileCountX; col++) {
|
for (int col = 0; col < world.tileCountX; col++) {
|
||||||
real32 fill = getTileValueUnchecked(&world, currMap, col, row) == 1 ? 1.0f : 0.5f;
|
real32 fill = getTileValueUnchecked(&world, currMap, col, row) == 1 ? 1.0f : 0.5f;
|
||||||
real32 minX = world.upperLeftX + ((real32)col)*world.tileWidth;
|
real32 minX = world.upperLeftX + ((real32)col)*world.tileSideInPixels;
|
||||||
real32 minY = world.upperLeftY + ((real32)row)*world.tileHeight;
|
real32 minY = world.upperLeftY + ((real32)row)*world.tileSideInPixels;
|
||||||
real32 maxX = minX + world.tileWidth;
|
real32 maxX = minX + world.tileSideInPixels;
|
||||||
real32 maxY = minY + world.tileHeight;
|
real32 maxY = minY + world.tileSideInPixels;
|
||||||
drawRectangle(videoBuf, minX, minY, maxX, maxY, fill, fill, fill);
|
drawRectangle(videoBuf, minX, minY, maxX, maxY, fill, fill, fill);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
real32 playerLeft = state->playerX - 0.5f * playerWidth;
|
real32 playerX = world.upperLeftX + state->playerPos.x + state->playerPos.tileX * world.tileSideInPixels;
|
||||||
real32 playerTop = state->playerY - playerHeight;
|
real32 playerY = world.upperLeftY + state->playerPos.y + state->playerPos.tileY * world.tileSideInPixels;
|
||||||
drawRectangle(videoBuf, playerLeft, playerTop, state->playerX + playerWidth*0.5f, state->playerY, playerR, playerG, playerB);
|
real32 playerLeft = playerX - 0.5f * playerWidth;
|
||||||
|
real32 playerTop = playerY - playerHeight;
|
||||||
|
drawRectangle(videoBuf, playerLeft, playerTop, playerX + playerWidth*0.5f, playerY, playerR, playerG, playerB);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" GAME_GET_SOUND_SAMPLES(gameGetSoundSamples) {
|
extern "C" GAME_GET_SOUND_SAMPLES(gameGetSoundSamples) {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
@@ -182,13 +181,14 @@ struct RawPosition {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct World {
|
struct World {
|
||||||
|
real32 tileSideInMeters;
|
||||||
|
int32 tileSideInPixels;
|
||||||
|
|
||||||
int32 tileMapCountX;
|
int32 tileMapCountX;
|
||||||
int32 tileMapCountY;
|
int32 tileMapCountY;
|
||||||
|
|
||||||
real32 upperLeftX;
|
real32 upperLeftX;
|
||||||
real32 upperLeftY;
|
real32 upperLeftY;
|
||||||
real32 tileWidth;
|
|
||||||
real32 tileHeight;
|
|
||||||
|
|
||||||
int32 tileCountY;
|
int32 tileCountY;
|
||||||
int32 tileCountX;
|
int32 tileCountX;
|
||||||
@@ -197,10 +197,7 @@ struct World {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct GameState {
|
struct GameState {
|
||||||
int32 tileMapX;
|
CanonicalPosition playerPos;
|
||||||
int32 tileMapY;
|
|
||||||
real32 playerX;
|
|
||||||
real32 playerY;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// === Game to platform services ===
|
// === Game to platform services ===
|
||||||
|
|||||||
33
src/handmade_intrinsics.h
Normal file
33
src/handmade_intrinsics.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "handmade.h"
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
#define PI32 3.141592653589f
|
||||||
|
|
||||||
|
inline int32 roundReal32ToUInt32(real32 realNum) {
|
||||||
|
return (uint32)(realNum + 0.5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int32 roundReal32ToInt32(real32 realNum) {
|
||||||
|
return (int32)(realNum + 0.5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int32 floorReal32ToInt32(real32 realNum) {
|
||||||
|
return (int32)floorf(realNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int32 truncateReal32ToInt32(real32 realNum) {
|
||||||
|
return (int32)realNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline real32 sin(real32 angle) {
|
||||||
|
return sinf(angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline real32 cos(real32 angle) {
|
||||||
|
return cosf(angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline real32 atan2(real32 y, real32 x) {
|
||||||
|
return atan2f(y, x);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user