Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "anonhero.h"
- #include "anontile.cpp"
- extern "C"
- GAME_GET_SOUND_SAMPLES(GameGetSoundSamples) {
- game_state *state = (game_state *)memory->permanentStorage;
- GameOutputSound(soundBuffer, state);
- }
- void GameOutputSound(game_sound_buffer *sBuffer, game_state *state) {
- int16 *sampleOut = sBuffer->samples;
- for(int sampleIndex = 0;
- sampleIndex < sBuffer->sampleCount;
- ++sampleIndex) {
- int16 sampleValue = 0;
- *sampleOut++ = sampleValue;
- *sampleOut++ = sampleValue;
- }
- }
- internal void
- DrawRectangle(game_offscreen_buffer *screen, vector realMin,
- vector realMax, real32 r, real32 g, real32 b)
- {
- int32 minX = RoundReal32ToInt32(realMin.x);
- int32 minY = RoundReal32ToInt32(realMin.y);
- int32 maxX = RoundReal32ToInt32(realMax.x);
- int32 maxY = RoundReal32ToInt32(realMax.y);
- if(minX < 0) { minX = 0; }
- if(minY < 0) { minY = 0; }
- if(maxX > screen->width) { maxX = screen->width; }
- if(maxY > screen->height) { maxY = screen->height; }
- uint32 color = ( (RoundReal32ToUInt32(r * 255.0f) << 16) |
- (RoundReal32ToUInt32(g * 255.0f) << 8 ) |
- (RoundReal32ToUInt32(b * 255.0f) << 0 )
- );
- uint8 *row = ((uint8 *)screen->memory +
- minX * screen->bytesPerPixel +
- minY * screen->pitch);
- for(int y = minY; y < maxY; ++y)
- {
- uint32 *pixel = (uint32 *)row;
- for(int x = minX; x < maxX; ++x)
- {
- *pixel++ = color;
- }
- row += screen->pitch;
- }
- }
- #pragma pack(push, 1)
- struct bitmap_header
- {
- uint16 fileType;
- uint32 fileSize;
- uint16 reservedOne;
- uint16 reservedTwo;
- uint32 bitmapOffset;
- uint32 size;
- int32 width;
- int32 height;
- uint16 planes;
- uint16 bitsPerPixel;
- uint32 compression;
- uint32 sizeOfBitmap;
- int32 horzResolution;
- int32 vertResolution;
- uint32 colorsUsed;
- uint32 colorsImportant;
- uint32 redMask;
- uint32 greenMask;
- uint32 blueMask;
- };
- #pragma pack(pop)
- internal loaded_bitmap
- LoadBitmap(thread_context *thread, debug_platform_read_entire_file *ReadEntireFile, char *fileName)
- {
- loaded_bitmap bitmap = {};
- debug_read_file_result readResult = ReadEntireFile(thread, fileName);
- if(readResult.contentSize != 0)
- {
- bitmap_header *header = (bitmap_header *)readResult.contents;
- uint32 *pixels = (uint32 *)((uint8 *)readResult.contents + header->bitmapOffset);
- bitmap.pixels = pixels;
- bitmap.width = header->width;
- bitmap.height = header->height;
- if(header->compression == 3)
- {
- uint32 redMask = header->redMask;
- uint32 greenMask = header->greenMask;
- uint32 blueMask = header->blueMask;
- uint32 alphaMask = ~(redMask | greenMask | blueMask);
- bit_scan_result redShift = FindLeastSignificantSetBit(redMask);
- bit_scan_result greenShift = FindLeastSignificantSetBit(greenMask);
- bit_scan_result blueShift = FindLeastSignificantSetBit(blueMask);
- bit_scan_result alphaShift = FindLeastSignificantSetBit(alphaMask);
- Assert(redShift.found);
- Assert(greenShift.found);
- Assert(blueShift.found);
- Assert(alphaShift.found);
- uint32 *sourceDest = pixels;
- for(int32 y = 0; y < header->height; ++y)
- {
- for(int32 x = 0; x < header->width; ++x)
- {
- uint32 col = *sourceDest;
- *sourceDest++ = ((((col >> alphaShift.index) & 0xff) << 24) |
- (((col >> redShift.index) & 0xff) << 16) |
- (((col >> greenShift.index) & 0xff) << 8) |
- (((col >> blueShift.index) & 0xff) << 0));
- }
- }
- }
- }
- return(bitmap);
- }
- internal void
- DrawBitmap(game_offscreen_buffer *buffer, loaded_bitmap *bitmap, vector pos, int32 alignX = 0, int32 alignY = 0)
- {
- pos.x -= (real32)alignX;
- pos.y -= (real32)alignY;
- int32 minX = RoundReal32ToInt32(pos.x);
- int32 minY = RoundReal32ToInt32(pos.y);
- int32 maxX = minX + bitmap->width;
- int32 maxY = minY + bitmap->height;
- int32 sourceOffsetX = 0;
- if(minX < 0)
- {
- sourceOffsetX = -minX;
- minX = 0;
- }
- int32 sourceOffsetY = 0;
- if(minY < 0)
- {
- sourceOffsetY = -minY;
- minY = 0;
- }
- if(maxX > buffer->width) { maxX = buffer->width; }
- if(maxY > buffer->height) { maxY = buffer->height; }
- uint32 *sourceRow = bitmap->pixels + bitmap->width * (bitmap->height - 1);
- sourceRow += -sourceOffsetY * bitmap->width + sourceOffsetX;
- uint8 *destRow = ((uint8 *)buffer->memory +
- minX * buffer->bytesPerPixel +
- minY * buffer->pitch);
- for(int y = minY; y < maxY; ++y)
- {
- uint32 *dest = (uint32 *)destRow;
- uint32 *source = sourceRow;
- for(int x = minX; x < maxX; ++x)
- {
- real32 alpha = (real32)((*source >> 24) & 0xff) /255.0f;
- real32 sourceRed = (real32)((*source >> 16) & 0xff);
- real32 sourceGreen = (real32)((*source >> 8) & 0xff);
- real32 sourceBlue = (real32)((*source >> 0) & 0xff);
- real32 destRed = (real32)((*dest >> 16) & 0xff);
- real32 destGreen = (real32)((*dest >> 8) & 0xff);
- real32 destBlue = (real32)((*dest >> 0) & 0xff);
- real32 r = alpha + (1.0f - alpha) * destRed + alpha * sourceRed;
- real32 g = alpha + (1.0f - alpha) * destGreen + alpha * sourceGreen;
- real32 b = alpha + (1.0f - alpha) * destBlue + alpha * sourceBlue;
- *dest = ( (uint32)(r + 0.5f) << 16 |
- (uint32)(g + 0.5f) << 8 |
- (uint32)(b + 0.5f) << 0 );
- ++dest;
- ++source;
- }
- destRow += buffer->pitch;
- sourceRow -= bitmap->width;
- }
- }
- internal void
- ChangeEntityResidence(game_state* state, uint32 entityIndex, entity_residence residence)
- {
- if(residence == EntityResidenceHigh)
- {
- if(state->entityResidence[entityIndex] != EntityResidenceHigh)
- {
- high_entity *highEntity = &state->highEntities[entityIndex];
- dormant_entity *dormantEntity = &state->dormantEntities[entityIndex];
- tile_map_difference diff = SubtractInReal32(state->world->tileMap, &dormantEntity->tilePos, &state->cameraPos);
- highEntity->pos = diff.dxy;
- highEntity->vel = Vector(0);
- highEntity->absTileZ = dormantEntity->tilePos.absTileZ;
- highEntity->facingDirection = 0;
- }
- }
- state->entityResidence[entityIndex] = residence;
- }
- inline entity
- GetEntity(game_state *state, entity_residence residence, uint32 index)
- {
- entity ent = {};
- if((index > 0) && (index < state->entityCount))
- {
- if(state->entityResidence[index] < residence)
- {
- ChangeEntityResidence(state, index, residence);
- Assert(state->entityResidence[index] >= residence);
- }
- ent.residence = residence;
- ent.dormant = &state->dormantEntities[index];
- ent.low = &state->lowEntities[index];
- ent.high = &state->highEntities[index];
- }
- return ent;
- }
- internal void
- InitializePlayer(game_state *state, uint32 entityIndex)
- {
- entity ent = GetEntity(state, EntityResidenceDormant, entityIndex);
- ent.dormant->tilePos.absTileX = 2;
- ent.dormant->tilePos.absTileY = 3;
- ent.dormant->tilePos.offset.x = 0.0f;
- ent.dormant->tilePos.offset.y = 0.0f;
- ent.dormant->height = 0.5f;
- ent.dormant->width = 1.0f;
- ent.dormant->collides = true;
- ent.high->facingDirection = 0;
- ChangeEntityResidence(state, entityIndex, EntityResidenceHigh);
- if(GetEntity(state, EntityResidenceHigh, state->cameraFollowingEntityIndex).residence == EntityResidenceNonexistent)
- {
- state->cameraFollowingEntityIndex = entityIndex;
- }
- }
- internal uint32
- AddEntity(game_state *state, entity_type type){
- uint32 entityIndex = state->entityCount++;
- Assert(state->entityCount < ArrayCount(state->dormantEntities));
- Assert(state->entityCount < ArrayCount(state->lowEntities));
- Assert(state->entityCount < ArrayCount(state->highEntities));
- state->entityResidence[entityIndex] = EntityResidenceDormant;
- state->dormantEntities[entityIndex] = {};
- state->lowEntities[entityIndex] = {};
- state->highEntities[entityIndex] = {};
- state->dormantEntities[entityIndex].type = type;
- return entityIndex;
- }
- internal uint32
- AddWall(game_state *state, uint32 absTileX, uint32 absTileY, uint32 absTileZ)
- {
- uint32 entityIndex = AddEntity(state, EntityType_Wall);
- entity ent = GetEntity(state, EntityResidenceDormant, entityIndex);
- ent.dormant->tilePos.absTileX = absTileX;
- ent.dormant->tilePos.absTileY = absTileY;
- ent.dormant->tilePos.absTileZ = absTileZ;
- ent.dormant->height = state->world->tileMap->tileSideInMeters;
- ent.dormant->width = ent.dormant->height;
- ent.dormant->collides = true;
- return entityIndex;
- }
- internal uint32
- AddPlayer(game_state *state)
- {
- uint32 entityIndex = AddEntity(state, EntityType_Player);
- entity ent = GetEntity(state, EntityResidenceDormant, entityIndex);
- ent.dormant->tilePos.absTileX = 1;
- ent.dormant->tilePos.absTileY = 3;
- ent.dormant->tilePos.offset.x = 0;
- ent.dormant->tilePos.offset.y = 0;
- ent.dormant->height = 0.5f;
- ent.dormant->width = 1.0f;
- ent.dormant->collides = true;
- ChangeEntityResidence(state, entityIndex, EntityResidenceHigh);
- if(GetEntity(state, EntityResidenceDormant, state->cameraFollowingEntityIndex).residence == EntityResidenceNonexistent)
- {
- state->cameraFollowingEntityIndex = entityIndex;
- }
- return entityIndex;
- }
- internal bool
- TestWall(real32 wallX, real32 relX, real32 relY, real32 playerDeltaX, real32 playerDeltaY, real32 *tMin, real32 minY, real32 maxY)
- {
- bool hit = false;
- real32 epsilon = 0.001f;
- if(playerDeltaX != 0.0f)
- {
- real32 tResult = (wallX - relX) / playerDeltaX;
- real32 y = relY + tResult * playerDeltaY;
- if((tResult >= 0.0f) && (*tMin > tResult))
- {
- if(y >= minY && y <= maxY)
- {
- *tMin = Maximum(0.0f, tResult - epsilon);
- hit = true;
- }
- }
- }
- return hit;
- }
- internal void
- MovePlayer(game_state *state, entity ent, real32 dt, vector acceleration)
- {
- tile_map *tileMap = state->world->tileMap;
- real32 accLen = LengthSquared(acceleration);
- if(accLen > 1.0f)
- {
- acceleration *= (1.0f / SquareRoot(accLen));
- }
- real32 playerSpeed = 50.0f;
- acceleration *= playerSpeed;
- high_entity *highEntity = ent.high;
- acceleration+= -8.0f * highEntity->vel;
- vector oldPlayerPos = highEntity->pos;
- vector playerDelta = 0.5f * acceleration * Square(dt) + highEntity->vel * dt;
- highEntity->vel = acceleration * dt + highEntity->vel;
- vector newPlayerPos = oldPlayerPos + playerDelta;
- /*
- uint32 minTileX = Minimum(oldPlayerPos.absTileX, newPlayerPos.absTileX);
- uint32 minTileY = Minimum(oldPlayerPos.absTileY, newPlayerPos.absTileY);
- uint32 maxTileX = Maximum(oldPlayerPos.absTileX, newPlayerPos.absTileX);
- uint32 maxTileY = Maximum(oldPlayerPos.absTileY, newPlayerPos.absTileY);
- uint32 entityTileWidth = CeilReal32ToInt32(entity.dormant->width / tileMap->tileSideInMeters);
- uint32 entityTileHeight = CeilReal32ToInt32(entity.dormant->height / tileMap->tileSideInMeters);
- minTileX -= entityTileWidth;
- minTileY -= entityTileHeight;
- maxTileX += entityTileWidth;
- maxTileY += entityTileHeight;
- uint32 absTileZ = highEntity->pos.absTileZ;
- */
- for(uint32 iteration = 0; iteration < 4; iteration++)
- {
- real32 tMin = 1.0f;
- vector wallNormal = Vector(0, 0);
- uint32 hitEntityIndex = 0;
- vector desiredPosition = highEntity->pos + playerDelta;
- for(uint32 entityIndex = 1; entityIndex < state->entityCount; ++entityIndex)
- {
- entity testEntity = GetEntity(state, EntityResidenceDormant, entityIndex);
- if(testEntity.high != ent.high)
- {
- if(testEntity.dormant->collides)
- {
- real32 diameterW = testEntity.dormant->width + ent.dormant->width;
- real32 diameterH = testEntity.dormant->height + ent.dormant->height;
- vector minCorner = Vector(diameterW, diameterH) * -0.5f;
- vector maxCorner = Vector(diameterW, diameterH) * 0.5f;
- vector rel = highEntity->pos - testEntity.high->pos;
- if(TestWall(minCorner.x, rel.x, rel.y, playerDelta.x, playerDelta.y, &tMin, minCorner.y, maxCorner.y))
- {
- wallNormal = Vector(-1, 0);
- hitEntityIndex = entityIndex;
- }
- if(TestWall(maxCorner.x, rel.x, rel.y, playerDelta.x, playerDelta.y, &tMin, minCorner.y, maxCorner.y))
- {
- wallNormal = Vector(1, 0);
- hitEntityIndex = entityIndex;
- }
- if(TestWall(minCorner.y, rel.y, rel.x, playerDelta.y, playerDelta.x, &tMin, minCorner.x, maxCorner.x))
- {
- wallNormal = Vector(0, -1);
- hitEntityIndex = entityIndex;
- }
- if(TestWall(maxCorner.y, rel.y, rel.x, playerDelta.y, playerDelta.x, &tMin, minCorner.x, maxCorner.x))
- {
- wallNormal = Vector(0, 1);
- hitEntityIndex = entityIndex;
- }
- }
- }
- }
- highEntity->pos += tMin * playerDelta;
- if(hitEntityIndex)
- {
- highEntity->vel = highEntity->vel - 1 * Dot(highEntity->vel, wallNormal) * wallNormal;
- playerDelta = desiredPosition - highEntity->pos;
- playerDelta = playerDelta - 1 * Dot(playerDelta, wallNormal) * wallNormal;
- entity hitEntity = GetEntity(state, EntityResidenceDormant, hitEntityIndex);
- highEntity->absTileZ += hitEntity.dormant->dAbsTileZ;
- }
- else
- {
- break;
- }
- }
- if((highEntity->vel.x == 0.0f) && (highEntity->vel.y == 0.0f))
- {
- }
- else if(AbsoluteValue(highEntity->vel.x) > AbsoluteValue(highEntity->vel.y))
- {
- if(highEntity->vel.x > 0)
- {
- highEntity->facingDirection = 3;
- }
- else
- {
- highEntity->facingDirection = 2;
- }
- }
- else
- {
- if(highEntity->vel.y > 0)
- {
- highEntity->facingDirection = 1;
- }
- else
- {
- highEntity->facingDirection = 0;
- }
- }
- ent.dormant->tilePos = MapIntoTileSpace(state->world->tileMap, state->cameraPos, ent.high->pos);
- }
- internal void
- SetCamera(game_state *state, tile_map_position newCameraPos)
- {
- tile_map *tileMap = state->world->tileMap;
- tile_map_difference dCameraPos = SubtractInReal32(tileMap, &newCameraPos, &state->cameraPos);
- state->cameraPos = newCameraPos;
- uint32 tileSpanX = 17 * 3;
- uint32 tileSpanY = 9 * 3;
- rectangle cameraBounds = RectCenterDim(Vector(0, 0),
- tileMap->tileSideInMeters * Vector((real32)tileSpanX, (real32)tileSpanY));
- vector entityOffsetForFrame = -dCameraPos.dxy;
- for (uint32 entityIndex = 1; entityIndex < ArrayCount(state->highEntities); entityIndex++)
- {
- if(state->entityResidence[entityIndex] == EntityResidenceHigh)
- {
- high_entity *high = state->highEntities + entityIndex;
- high->pos += entityOffsetForFrame;
- if(!InRectangle(cameraBounds, high->pos))
- {
- ChangeEntityResidence(state, entityIndex, EntityResidenceDormant);
- }
- }
- }
- uint32 minTileX = newCameraPos.absTileX - tileSpanX / 2;
- uint32 maxTileX = newCameraPos.absTileX + tileSpanX / 2;
- uint32 minTileY = newCameraPos.absTileY - tileSpanY / 2;
- uint32 maxTileY = newCameraPos.absTileY + tileSpanY / 2;
- for(uint32 entityIndex = 1; entityIndex < ArrayCount(state->dormantEntities);++entityIndex)
- {
- if(state->entityResidence[entityIndex] == EntityResidenceDormant)
- {
- dormant_entity *dormant = state->dormantEntities + entityIndex;
- if((dormant->tilePos.absTileZ == newCameraPos.absTileZ) &&
- (dormant->tilePos.absTileX >= minTileX) && (dormant->tilePos.absTileX <= maxTileX) &&
- (dormant->tilePos.absTileY <= minTileY) && (dormant->tilePos.absTileY >= maxTileY))
- {
- ChangeEntityResidence(state, entityIndex, EntityResidenceHigh);
- }
- }
- }
- }
- extern "C"
- GAME_UPDATE_AND_RENDER(GameUpdateAndRender)
- {
- Assert(sizeof(game_state) <= memory->permanentStorageSize);
- game_state *state = (game_state *)memory->permanentStorage;
- if(!memory->isInitialized)
- {
- AddEntity(state, EntityType_NULL);
- state->background = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_background.bmp");
- state->shadow = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_shadow.bmp");
- player_bitmaps *bitmap;
- bitmap = state->playerBitmaps;
- bitmap->head = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_front_head.bmp");
- bitmap->cape = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_front_cape.bmp");
- bitmap->torso = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_front_torso.bmp");
- bitmap->alignX = 72;
- bitmap->alignY = 182;
- ++bitmap;
- bitmap->head = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_back_head.bmp");
- bitmap->cape = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_back_cape.bmp");
- bitmap->torso = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_back_torso.bmp");
- bitmap->alignX = 72;
- bitmap->alignY = 182;
- ++bitmap;
- bitmap->head = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_left_head.bmp");
- bitmap->cape = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_left_cape.bmp");
- bitmap->torso = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_left_torso.bmp");
- bitmap->alignX = 72;
- bitmap->alignY = 182;
- ++bitmap;
- bitmap->head = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_right_head.bmp");
- bitmap->cape = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_right_cape.bmp");
- bitmap->torso = LoadBitmap(thread, memory->DEBUGPlatformReadEntireFile, "../data/test_hero_right_torso.bmp");
- bitmap->alignX = 72;
- bitmap->alignY = 182;
- InitializeArena(&state->worldArena, memory->permanentStorageSize - sizeof(game_state),
- (uint8 *)memory->permanentStorage + sizeof(game_state));
- state->world = PushStruct(&state->worldArena, world_struct);
- world_struct *world = state->world;
- world->tileMap = PushStruct(&state->worldArena, tile_map);
- tile_map *tileMap = world->tileMap;
- tileMap->tileSideInMeters = 1.4f;
- tileMap->chunkShift = 4;
- tileMap->chunkMask = (1 << tileMap->chunkShift) - 1;
- tileMap->chunkDim = (1 << tileMap->chunkShift);
- tileMap->tileChunkCountX = 128;
- tileMap->tileChunkCountY = 128;
- tileMap->tileChunkCountZ = 2;
- tileMap->tileChunks = PushArray(&state->worldArena, tileMap->tileChunkCountX *
- tileMap->tileChunkCountY *
- tileMap->tileChunkCountZ, tile_chunk);
- uint32 tilesPerWidth = 17;
- uint32 tilesPerHeight = 9;
- //uint32 screenX = INT32_MAX / 2;
- //uint32 screenY = INT32_MAX / 2;
- uint32 screenX = 0;
- uint32 screenY = 0;
- uint32 randomNumberIndex = 0;
- global_var uint32 randomNumberTable[] =
- {
- 8314, 44235, 21701, 17516, 43092, 33335, 97299, 22848, 80141,
- 61410, 48723, 4181, 24414, 45859, 87211, 65608, 76049, 18534,
- 91995, 97499, 33980, 2504, 3073, 34617, 98205, 92770, 19378,
- 78030, 68726, 89330, 43046, 27738, 44474, 93254, 73010, 84810,
- 27585, 2405, 9268, 47108, 16991, 17851, 50754, 15701, 11624,
- 40660, 99603, 90590, 13621, 68696, 54747, 72842, 21912, 3027,
- 16186, 60320, 36499, 43653, 82852, 54094, 57380, 90575, 56881,
- 54163, 83489, 44859, 37205, 61595, 16961, 4371, 23824, 74254,
- 56728, 70177, 15671, 93208, 1722, 43347, 14880, 18458, 54079,
- 21744, 20385, 51070, 12731, 6436, 32185, 88909, 77678, 97423,
- 49211, 33044, 48921, 94705, 73701, 83474, 25390, 99350, 4287,
- 53173, 83389, 44859, 37205, 71595, 17961, 4171, 21824, 74254,
- 56728, 70177, 15671, 91208, 1722, 41147, 14880, 18458, 54079,
- 21744, 20185, 51070, 12711, 6416, 12185, 8909, 77678, 7423,
- 69211, 33066, 68921, 96705, 73601, 83674, 23390, 99350, 12354
- };
- uint32 absTileZ = 0;
- bool32 doorLeft = false;
- bool32 doorRight = false;
- bool32 doorTop = false;
- bool32 doorBottom = false;
- bool32 doorUp = false;
- bool32 doorDown = false;
- for(uint32 screenIndex = 0; screenIndex < 2; ++screenIndex)
- {
- Assert(randomNumberIndex < ArrayCount(randomNumberTable));
- uint32 randomChoice = randomNumberTable[randomNumberIndex++] % 2;
- if(doorUp || doorDown)
- {
- randomChoice = randomNumberTable[randomNumberIndex++] % 2;
- }
- #if 0
- else
- {
- randomChoice = randomNumberTable[randomNumberIndex++] % 3;
- }
- #endif
- bool32 createdZDoor = false;
- if(randomChoice == 2)
- {
- createdZDoor = true;
- if(absTileZ == 0)
- {
- doorUp = true;
- }
- else
- {
- doorDown = true;
- }
- }
- else if(randomChoice == 1)
- {
- doorRight = true;
- }
- else
- {
- doorTop = true;
- }
- for(uint32 tileY = 0; tileY < tilesPerHeight; ++tileY)
- {
- for(uint32 tileX = 0; tileX < tilesPerWidth; ++tileX)
- {
- uint32 absTileX = screenX * tilesPerWidth + tileX;
- uint32 absTileY = screenY * tilesPerHeight + tileY;
- uint32 tileValue = 1;
- if(tileX == 0 && (!doorLeft || (tileY != tilesPerHeight / 2)))
- {
- tileValue = 2;
- }
- if(tileX == (tilesPerWidth - 1) && (!doorRight || (tileY != tilesPerHeight / 2)))
- {
- tileValue = 2;
- }
- if(tileY == 0 && (!doorBottom || (tileX != tilesPerWidth / 2)))
- {
- tileValue = 2;
- }
- if(tileY == (tilesPerHeight - 1) && (!doorTop || (tileX != tilesPerWidth / 2)))
- {
- tileValue = 2;
- }
- if(tileX == 10 && tileY == 6)
- {
- if(doorUp)
- {
- tileValue = 3;
- }
- if(doorDown)
- {
- tileValue = 4;
- }
- }
- SetTileValue(&state->worldArena, world->tileMap, absTileX, absTileY, absTileZ, tileValue);
- if(tileValue == 2)
- {
- AddWall(state, absTileX, absTileY, absTileZ);
- }
- }
- }
- doorLeft = doorRight;
- doorBottom = doorTop;
- if(createdZDoor)
- {
- doorDown = !doorDown;
- doorUp = !doorUp;
- }
- else
- {
- doorUp = false;
- doorDown = false;
- }
- doorRight = false;
- doorTop = false;
- if(randomChoice == 2)
- {
- if(absTileZ == 0)
- {
- absTileZ = 1;
- }
- else
- {
- absTileZ = 0;
- }
- }
- else if(randomChoice == 1)
- {
- screenX++;
- }
- else
- {
- screenY++;
- }
- }
- tile_map_position newCameraPos = {};
- newCameraPos.absTileX = 17/2;
- newCameraPos.absTileY = 9/2;
- SetCamera(state, newCameraPos);
- memory->isInitialized = true;
- }
- world_struct *world = state->world;
- tile_map *tileMap = world->tileMap;
- int32 tileSideInPixels = 60;
- real32 metersToPixels = (real32)tileSideInPixels / tileMap->tileSideInMeters;
- real32 lowerLeftX = -(real32)tileSideInPixels / 2;
- real32 lowerLeftY = (real32)buffer->height;
- for(int controllerIndex = 0; controllerIndex < ArrayCount(input->controllers); ++controllerIndex)
- {
- game_controller_input *controller = GetController(input, controllerIndex);
- entity controllingEntity = GetEntity(state, EntityResidenceHigh, state->playerIndexForController[controllerIndex]);
- if(controllingEntity.residence != EntityResidenceNonexistent)
- {
- state->cameraFollowingEntityIndex = state->playerIndexForController[controllerIndex];
- vector acceleration = {}; //ddPlayer in handmade
- if(controller->isAnalog) {
- //acceleration = Vector(controller->stickAverageX, controller->stickAverageY) // No gamepad code yet!
- }
- else
- {
- if (controller->moveDown.endedDown)
- {
- acceleration.y = -1.0f;
- }
- if (controller->moveUp.endedDown)
- {
- acceleration.y = 1.0f;
- }
- if (controller->moveLeft.endedDown)
- {
- acceleration.x = -1.0f;
- }
- if (controller->moveRight.endedDown)
- {
- acceleration.x = 1.0f;
- }
- }
- if(controller->actionUp.endedDown)
- {
- controllingEntity.high->dz = 3.5f;
- }
- MovePlayer(state, controllingEntity, input->dt, acceleration);
- }
- else
- {
- if(controller->start.endedDown)
- {
- uint32 entityIndex = AddPlayer(state);
- // TEST: controllingEntity = GetEntity(state, EntityResidenceDormant, entityIndex);
- // TEST: InitializePlayer(state, entityIndex);
- state->playerIndexForController[controllerIndex] = entityIndex;
- }
- }
- }
- vector entityOffset = {};
- entity cameraFollowingEntity = GetEntity(state, EntityResidenceHigh, state->cameraFollowingEntityIndex);
- if(cameraFollowingEntity.residence != EntityResidenceNonexistent)
- {
- tile_map_position newCameraPos = state->cameraPos;
- newCameraPos.absTileZ = cameraFollowingEntity.dormant->tilePos.absTileZ;
- #if 1
- if(cameraFollowingEntity.high->pos.x > (9.0f * tileMap->tileSideInMeters))
- {
- newCameraPos.absTileX += 17;
- }
- if(cameraFollowingEntity.high->pos.x < -(9.0f * tileMap->tileSideInMeters))
- {
- newCameraPos.absTileX -= 17;
- }
- if(cameraFollowingEntity.high->pos.y > (5.0f * tileMap->tileSideInMeters))
- {
- newCameraPos.absTileY += 9;
- }
- if(cameraFollowingEntity.high->pos.y < -(5.0f * tileMap->tileSideInMeters))
- {
- newCameraPos.absTileY -= 9;
- }
- #else
- if(cameraFollowingEntity.high->pos.X > (1.0f*TileMap->TileSideInMeters))
- {
- newCameraPos.absTileX += 1;
- }
- if(cameraFollowingEntity.high->pos.X < -(1.0f*TileMap->TileSideInMeters))
- {
- newCameraPos.absTileX -= 1;
- }
- if(cameraFollowingEntity.high->pos.Y > (1.0f*TileMap->TileSideInMeters))
- {
- newCameraPos.absTileY += 1;
- }
- if(cameraFollowingEntity.high->pos.Y < -(1.0f*TileMap->TileSideInMeters))
- {
- newCameraPos.absTileY -= 1;
- }
- #endif
- SetCamera(state, newCameraPos);
- }
- DrawBitmap(buffer, &state->background, Vector(0));
- //DrawRectangle(buffer, 0.0f, 0.0f, (real32)buffer->width, (real32)buffer->height, 1.0f, 0.0f, 1.0f); //Clear
- vector screenCenter = { (real32)buffer->width * 0.5f,
- (real32)buffer->height * 0.5f };
- #if 0
- for(int32 relRow = -10; relRow < 10; ++relRow)
- {
- for(int32 relColumn = -20; relColumn < 20; ++relColumn)
- {
- uint32 column = state->cameraPos.absTileX + relColumn;
- uint32 row = state->cameraPos.absTileY + relRow;
- uint32 layer = state->cameraPos.absTileZ;
- uint32 tileId = GetTileValue(tileMap, column, row, layer);
- if(tileId > 1)
- {
- real32 gray = 0.5f;
- if(tileId == 2)
- {
- gray = 1.0f;
- }
- if(tileId > 2)
- {
- gray = 0.75f;
- }
- if((row == state->cameraPos.absTileY) && (column == state->cameraPos.absTileX))
- {
- gray = 0.0f;
- }
- vector center = { screenCenter.x - metersToPixels * state->cameraPos.offset.x + ((real32)relColumn) * tileSideInPixels,
- screenCenter.y + metersToPixels * state->cameraPos.offset.y - ((real32)relRow) * tileSideInPixels };
- real32 halfTileSideInPixels = 0.5f * tileSideInPixels;
- vector min = center - Vector(halfTileSideInPixels);
- vector max = center + Vector(halfTileSideInPixels);
- DrawRectangle(buffer, min, max, gray, gray, gray);
- }
- }
- }
- #endif
- for(uint32 entityIndex = 1; entityIndex < state->entityCount; ++entityIndex)
- {
- if(state->entityResidence[entityIndex] == EntityResidenceHigh)
- {
- high_entity *highEntity = &state->highEntities[entityIndex];
- dormant_entity *dormantEntity = &state->dormantEntities[entityIndex];
- highEntity->pos += entityOffset;
- real32 dt = input->dt;
- real32 ddz = -9.8f;
- real32 deltaZ = (0.5f * ddz * Square(dt) + highEntity->dz * dt);
- highEntity->z += deltaZ;
- highEntity->dz = ddz * dt + highEntity->dz;
- if(highEntity->z < 0)
- {
- highEntity->z = 0;
- }
- real32 playerR = 0.5f;
- real32 playerG = 0.15f;
- real32 playerB = 0.79f;
- vector playerGroundPoint = { screenCenter.x + metersToPixels * highEntity->pos.x,
- screenCenter.y - metersToPixels * highEntity->pos.y };
- vector playerPixelDimensions = { metersToPixels * dormantEntity->width, metersToPixels * dormantEntity->height };
- vector playerLeftTop = { playerGroundPoint.x - 0.5f * metersToPixels * dormantEntity->width,
- playerGroundPoint.y - 0.5f * metersToPixels * dormantEntity->height };
- vector playerRightBot = playerLeftTop + playerPixelDimensions;
- real32 z = -metersToPixels * highEntity->z;
- vector jumpOffset = Vector(0, z);
- if(dormantEntity->type == EntityType_Player)
- {
- player_bitmaps *bitmaps = &state->playerBitmaps[highEntity->facingDirection];
- DrawBitmap(buffer, &state->shadow, playerGroundPoint, bitmaps->alignX, bitmaps->alignY);
- DrawBitmap(buffer, &bitmaps->torso, playerGroundPoint + jumpOffset, bitmaps->alignX, bitmaps->alignY);
- DrawBitmap(buffer, &bitmaps->cape, playerGroundPoint + jumpOffset, bitmaps->alignX, bitmaps->alignY);
- DrawBitmap(buffer, &bitmaps->head, playerGroundPoint + jumpOffset, bitmaps->alignX, bitmaps->alignY);
- }
- else
- {
- DrawRectangle(buffer, playerLeftTop, playerRightBot,
- playerR, playerG, playerB);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement