Browse Source

update

master
Ledda 16 hours ago
parent
commit
7b9b6f3200
8 changed files with 91 additions and 96 deletions
  1. +0
    -2
      misc/bdebug.bat
  2. +0
    -2
      misc/brun.bat
  3. +0
    -0
      misc/cmds/build.bat
  4. +1
    -0
      misc/cmds/run.bat
  5. +2
    -0
      misc/hh.bat
  6. +51
    -85
      src/handmade.cpp
  7. +4
    -7
      src/handmade.h
  8. +33
    -0
      src/handmade_intrinsics.h

+ 0
- 2
misc/bdebug.bat View File

@@ -1,2 +0,0 @@
call build || exit /b %errorlevel%
devenv .\build\handmade_win32.exe

+ 0
- 2
misc/brun.bat View File

@@ -1,2 +0,0 @@
call build || exit /b %errorlevel%
.\build\handmade_win32.exe

misc/build.bat → misc/cmds/build.bat View File


+ 1
- 0
misc/cmds/run.bat View File

@@ -0,0 +1 @@
.\build\handmade_win32.exe

+ 2
- 0
misc/hh.bat View File

@@ -0,0 +1,2 @@
call .\misc\cmds\%1.bat || exit /b %errorlevel%
.\build\handmade_win32.exe

+ 51
- 85
src/handmade.cpp View File

@@ -1,23 +1,5 @@
#include "handmade.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;
}
#include "handmade_intrinsics.h"

internal void drawRectangle(GameOffscreenBuffer *buffer, real32 realMinX, real32 realMinY, real32 realMaxX, real32 realMaxY, real32 R, real32 G, real32 B) {
int32 minX = roundReal32ToInt32(realMinX);
@@ -87,51 +69,39 @@ inline TileMap *getTileMap(World *world, int32 tileMapX, int32 tileMapY) {
return tileMap;
}

inline CanonicalPosition getCanonicalPosition(World* world, RawPosition pos) {
CanonicalPosition result = {};

result.tileMapX = pos.tileMapX;
result.tileMapY = pos.tileMapY;
inline void recanonicaliseOrd(World *world, int32 tileCount, int32 *tileMapOrd, int32 *tileOrd, real32* ord) {
int32 offset = floorReal32ToInt32(*ord / world->tileSideInPixels);
*tileOrd += offset;
*ord -= offset*world->tileSideInPixels;

real32 x = pos.x - world->upperLeftX;
real32 y = pos.y - world->upperLeftY;
Assert(*ord >= 0);
Assert(*ord < world->tileSideInPixels);

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 (*tileOrd < 0) {
*tileOrd += tileCount;
*tileMapOrd -= 1;
}

if (result.tileY < 0) {
result.tileY += world->tileCountY;
result.tileMapY -= 1;
if (*tileOrd >= tileCount) {
*tileOrd -= tileCount;
*tileMapOrd += 1;
}
}

if (result.tileX >= world->tileCountX) {
result.tileX -= world->tileCountX;
result.tileMapX += 1;
}
inline CanonicalPosition recanonicalisePosition(World* world, CanonicalPosition pos) {
CanonicalPosition result = pos;

if (result.tileY >= world->tileCountY) {
result.tileY -= world->tileCountY;
result.tileMapY += 1;
}
recanonicaliseOrd(world, world->tileCountX, &result.tileMapX, &result.tileX, &result.x);
recanonicaliseOrd(world, world->tileCountY, &result.tileMapY, &result.tileY, &result.y);

return result;
}

internal bool32 isWorldPointEmpty(World *world, RawPosition testPos) {
internal bool32 isWorldPointEmpty(World *world, CanonicalPosition testPos) {
bool32 isEmpty = false;

CanonicalPosition canPos = getCanonicalPosition(world, testPos);
TileMap *tileMap = getTileMap(world, canPos.tileMapX, canPos.tileMapY);

isEmpty = (getTileValueUnchecked(world, tileMap, canPos.tileX, canPos.tileY) == 0);
TileMap *tileMap = getTileMap(world, testPos.tileMapX, testPos.tileMapY);
isEmpty = (getTileValueUnchecked(world, tileMap, testPos.tileX, testPos.tileY) == 0);

return isEmpty;
}
@@ -142,11 +112,12 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) {
GameState *state = (GameState*)memory->permanentStorage;

if (!memory->isInitialised) {
state->playerPos.x = 150.0f;
state->playerPos.y = 150.0f;
state->playerPos.tileMapX = 0;
state->playerPos.tileMapY = 0;

memory->isInitialised = true;
state->playerX = 150.0f;
state->playerY = 150.0f;
state->tileMapX = 0;
state->tileMapY = 0;
}

const int TILEMAP_WIDTH = 16;
@@ -210,8 +181,8 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) {
world.tileMapCountY = WORLD_HEIGHT;
world.upperLeftX = 0;
world.upperLeftY = 0;
world.tileWidth = 60;
world.tileHeight = 60;
world.tileSideInMeters = 1.4f;
world.tileSideInPixels = 60;

TileMap maps[WORLD_HEIGHT][WORLD_WIDTH];
maps[0][0].tiles = (uint32*)tiles00;
@@ -256,29 +227,28 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) {
dPlayerX *= 96.0f;
dPlayerY *= 96.0f;

real32 newPlayerX = state->playerX + dPlayerX * input->dtForFrame;
real32 newPlayerY = state->playerY + dPlayerY * input->dtForFrame;

RawPosition playerPos = {
state->tileMapX,
state->tileMapY,
newPlayerX,
newPlayerY,
};
CanonicalPosition newPlayerPos = state->playerPos;
newPlayerPos.x += dPlayerX * input->dtForFrame;
newPlayerPos.y += dPlayerY * input->dtForFrame;
newPlayerPos = recanonicalisePosition(&world, newPlayerPos);

RawPosition playerBottomLeft = playerPos;
CanonicalPosition playerBottomLeft = newPlayerPos;
playerBottomLeft.x -= 0.5f * playerWidth;
playerBottomLeft = recanonicalisePosition(&world, playerBottomLeft);

RawPosition playerTopLeft = playerPos;
CanonicalPosition playerTopLeft = newPlayerPos;
playerTopLeft.x -= 0.5f * playerWidth;
playerTopLeft.y -= playerWidth;
playerTopLeft = recanonicalisePosition(&world, playerTopLeft);

RawPosition playerBottomRight = playerPos;
CanonicalPosition playerBottomRight = newPlayerPos;
playerBottomRight.x += 0.5f * playerWidth;
playerBottomRight = recanonicalisePosition(&world, playerBottomRight);

RawPosition playerTopRight = playerPos;
CanonicalPosition playerTopRight = newPlayerPos;
playerTopRight.x += 0.5f * playerWidth;
playerTopRight.y -= playerWidth;
playerTopRight = recanonicalisePosition(&world, playerTopRight);

if (
isWorldPointEmpty(&world, playerTopLeft) &&
@@ -286,34 +256,30 @@ extern "C" GAME_UPDATE_AND_RENDER(gameUpdateAndRender) {
isWorldPointEmpty(&world, playerBottomLeft) &&
isWorldPointEmpty(&world, playerBottomRight)
) {
CanonicalPosition canPos = getCanonicalPosition(&world, playerPos);

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;
state->playerPos = newPlayerPos;
}
}

// clearscreen
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 col = 0; col < world.tileCountX; col++) {
real32 fill = getTileValueUnchecked(&world, currMap, col, row) == 1 ? 1.0f : 0.5f;
real32 minX = world.upperLeftX + ((real32)col)*world.tileWidth;
real32 minY = world.upperLeftY + ((real32)row)*world.tileHeight;
real32 maxX = minX + world.tileWidth;
real32 maxY = minY + world.tileHeight;
real32 minX = world.upperLeftX + ((real32)col)*world.tileSideInPixels;
real32 minY = world.upperLeftY + ((real32)row)*world.tileSideInPixels;
real32 maxX = minX + world.tileSideInPixels;
real32 maxY = minY + world.tileSideInPixels;
drawRectangle(videoBuf, minX, minY, maxX, maxY, fill, fill, fill);
}
}

real32 playerLeft = state->playerX - 0.5f * playerWidth;
real32 playerTop = state->playerY - playerHeight;
drawRectangle(videoBuf, playerLeft, playerTop, state->playerX + playerWidth*0.5f, state->playerY, playerR, playerG, playerB);
real32 playerX = world.upperLeftX + state->playerPos.x + state->playerPos.tileX * world.tileSideInPixels;
real32 playerY = world.upperLeftY + state->playerPos.y + state->playerPos.tileY * world.tileSideInPixels;
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) {


+ 4
- 7
src/handmade.h View File

@@ -1,7 +1,6 @@
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>

#pragma once

@@ -182,13 +181,14 @@ struct RawPosition {
};

struct World {
real32 tileSideInMeters;
int32 tileSideInPixels;

int32 tileMapCountX;
int32 tileMapCountY;

real32 upperLeftX;
real32 upperLeftY;
real32 tileWidth;
real32 tileHeight;

int32 tileCountY;
int32 tileCountX;
@@ -197,10 +197,7 @@ struct World {
};

struct GameState {
int32 tileMapX;
int32 tileMapY;
real32 playerX;
real32 playerY;
CanonicalPosition playerPos;
};

// === Game to platform services ===


+ 33
- 0
src/handmade_intrinsics.h View 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);
}

Loading…
Cancel
Save