675 lines
24 KiB
C
675 lines
24 KiB
C
#include <SDL2/SDL.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
#include "util/font.h"
|
|
#include "util/audio.h"
|
|
#include "tiles/tile.h"
|
|
#include "items/item.h"
|
|
#include "stdlib.h"
|
|
#include "player/player.h"
|
|
#include "util/atlas.h"
|
|
#include "entity/entity.h"
|
|
#include "util/gamestate.h"
|
|
#include "util/button.h"
|
|
|
|
#define GAME_NAME "FactoCraft"
|
|
|
|
//Screen dimension constants
|
|
const int targetFPS = 60;
|
|
const int delayNeeded = 1000 / targetFPS;
|
|
|
|
#define biggerFont fonts[0]
|
|
#define smallFont fonts[1]
|
|
#define smallerFont fonts[2]
|
|
#define smallestFont fonts[3]
|
|
#define reallySmallestFont fonts[4]
|
|
|
|
bool largeScreen = false;
|
|
|
|
unsigned long frames = 0;
|
|
bool cursor = true;
|
|
|
|
void msleep(unsigned int milliseconds) {
|
|
struct timespec ts;
|
|
ts.tv_sec = milliseconds / 1000;
|
|
ts.tv_nsec = (milliseconds % 1000) * 1000000;
|
|
nanosleep(&ts, NULL);
|
|
}
|
|
|
|
SDL_GLContext glContext;
|
|
|
|
int init() {
|
|
//Initialize SDL
|
|
|
|
srand(time(NULL));
|
|
|
|
memset(tileMap, 0, sizeof(tileMap));
|
|
memset(&neededUpdates, 0, sizeof(neededUpdates));
|
|
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, NULL);
|
|
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1");
|
|
SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
|
|
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
|
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_CURSOR_VISIBLE, "1");
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
|
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
//Initialize SDL_ttf
|
|
if (TTF_Init() == -1) {
|
|
printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
|
|
return 1;
|
|
}
|
|
|
|
//Create window
|
|
window = SDL_CreateWindow(GAME_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DISPLAY_WIDTH,
|
|
DISPLAY_HEIGHT, SDL_WINDOW_SHOWN);
|
|
|
|
SDL_Surface *surf = IMG_Load("./assets/icon.png");
|
|
|
|
SDL_SetWindowIcon(window, surf);
|
|
SDL_FreeSurface(surf);
|
|
|
|
if (window == NULL) {
|
|
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
//Get window surface
|
|
mainRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
if (mainRenderer == NULL) {
|
|
printf("Renderer could not be created SDL_Error: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
// Create OpenGL context
|
|
glContext = SDL_GL_CreateContext(window);
|
|
if (!glContext) {
|
|
fprintf(stderr, "SDL_GL_CreateContext failed: %s\n", SDL_GetError());
|
|
exit(1);
|
|
}
|
|
|
|
// Use OpenGL context
|
|
SDL_GL_MakeCurrent(window, glContext); // Make sure OpenGL context is current before any OpenGL rendering
|
|
|
|
audioData.playerRect = &mainPlayer.rect;
|
|
|
|
SDL_AudioSpec spec = {0};
|
|
spec.freq = SAMPLE_RATE;
|
|
spec.format = AUDIO_F32;
|
|
spec.channels = 2;
|
|
spec.samples = 8192;
|
|
spec.callback = audio_callback;
|
|
spec.userdata = &audioData;
|
|
|
|
SDL_AudioDeviceID dev = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0);
|
|
if (dev == 0) {
|
|
printf("Failed to open audio: %s\n", SDL_GetError());
|
|
SDL_Quit();
|
|
}
|
|
|
|
for (int t = 0; t < MIDI_TRACK_MAX; t++) {
|
|
midiEventCount[t] = 0;
|
|
nextMidiEvent[t] = 0;
|
|
}
|
|
|
|
|
|
load_midi_file("assets/audio/testaid.mid");
|
|
load_midi_file("assets/audio/bg.mid");
|
|
|
|
SDL_PauseAudioDevice(dev, 0);
|
|
|
|
|
|
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
|
|
SDL_RenderClear(mainRenderer);
|
|
|
|
//generateTestMap();
|
|
|
|
setTileView(30,15);
|
|
|
|
SDL_SetRenderDrawBlendMode(mainRenderer, SDL_BLENDMODE_BLEND);
|
|
biggerFont = prepText(mainRenderer, 32, "assets/PublicPixel.ttf");
|
|
smallFont = prepText(mainRenderer, 16, "assets/PublicPixel.ttf");
|
|
smallerFont = prepText(mainRenderer, 12, "assets/PublicPixel.ttf");
|
|
smallestFont = prepText(mainRenderer, 8, "assets/PublicPixel.ttf");
|
|
reallySmallestFont = prepText(mainRenderer, 4, "assets/PublicPixel.ttf");
|
|
|
|
return 0;
|
|
}
|
|
|
|
bool lateInitDone = false;
|
|
|
|
void lateInit() {
|
|
if (lateInitDone) {
|
|
return;
|
|
}
|
|
lateInitDone = true;
|
|
initAtlas(mainRenderer);
|
|
|
|
loadBackgroundTiles(mainRenderer);
|
|
loadTiles(mainRenderer);
|
|
preSetupTiles();
|
|
loadItems(mainRenderer);
|
|
loadEntities(mainRenderer);
|
|
setupTiles();
|
|
initTiles();
|
|
|
|
initPlayer(&mainPlayer);
|
|
|
|
for (ItemType i = 0; i < 13; i++) {
|
|
mainPlayer.inventory.hotKeySlots[i] = i;
|
|
}
|
|
initWaveInfo(&waveInfo);
|
|
}
|
|
|
|
|
|
int render() {
|
|
|
|
renderAllTiles(mainRenderer, mainPlayer.rect);
|
|
renderEntities(mainRenderer, mainPlayer.rect);
|
|
renderPlayer(&mainPlayer);
|
|
|
|
|
|
SDL_RenderCopy(mainRenderer, backgroundTexture, &screenRect, &screenRect);
|
|
SDL_RenderCopy(mainRenderer, tilesTexture, &screenRect, &screenRect);
|
|
SDL_RenderCopy(mainRenderer, itemsTexture, &screenRect, &screenRect);
|
|
SDL_RenderCopy(mainRenderer, entityTexture, &screenRect, &screenRect);
|
|
SDL_RenderCopy(mainRenderer, hudTexture, &screenRect, &screenRect);
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int processEvent(SDL_Event e) {
|
|
if (e.type == SDL_QUIT) { return 0; }
|
|
else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_RESIZED) {
|
|
int newWidth = e.window.data1;
|
|
int newHeight = e.window.data2;
|
|
|
|
// Adjust the viewport to match the new window size;
|
|
if (newWidth > 1900 && newHeight > 1000) {
|
|
largeScreen = true;
|
|
} else {
|
|
largeScreen = false;
|
|
}
|
|
setTileView(largeScreen ? 60 : 30, largeScreen ? 31 : 15);
|
|
|
|
}
|
|
// }
|
|
|
|
if (e.type == SDL_KEYUP) {
|
|
if (e.key.keysym.mod & KMOD_ALT && e.key.keysym.scancode == SDL_SCANCODE_RETURN) {
|
|
largeScreen = !largeScreen;
|
|
setTileView(largeScreen ? 60 : 30, largeScreen ? 31 : 15);
|
|
}
|
|
}
|
|
|
|
switch (screenType) {
|
|
case SCREEN_GAME:
|
|
if (e.type == SDL_KEYDOWN) {
|
|
SDL_KeyCode keySym = e.key.keysym.sym;
|
|
SDL_Scancode scanCode = e.key.keysym.scancode;
|
|
SDL_Keymod keyMod = e.key.keysym.mod;
|
|
cursor = true;
|
|
switch (keySym) {
|
|
case SDLK_p:
|
|
speed = speed == 0 ? 0.004f : 0;
|
|
break;
|
|
case SDLK_r:
|
|
if (mainPlayer.inventory.activeSlotIndex == 0 && mainPlayer.cursor.canReach &&
|
|
mainPlayer.cursor.targetTile->type != TYPE_AIR) {
|
|
mainPlayer.cursor.direction = mainPlayer.cursor.targetTile->direction;
|
|
}
|
|
mainPlayer.cursor.direction = (mainPlayer.cursor.direction + 2) % ORIENT_DIRECTION_COUNT;
|
|
if (mainPlayer.inventory.activeSlotIndex == 0 && mainPlayer.cursor.canReach) {
|
|
mainPlayer.cursor.targetTile->direction = mainPlayer.cursor.direction;
|
|
}
|
|
break;
|
|
case SDLK_u:
|
|
laneTarget = !laneTarget;
|
|
break;
|
|
case SDLK_F3:
|
|
debugMode = !debugMode;
|
|
break;
|
|
case SDLK_F11:
|
|
printf("Enemy is at tile X:%d, Y:%d\n", entities.entities[0].tileRect.x,
|
|
entities.entities[0].tileRect.y);
|
|
break;
|
|
|
|
case SDLK_F4:
|
|
Tile *tile = &tileMap[playerTileY][playerTileX];
|
|
break;
|
|
case SDLK_LALT:
|
|
itemViewing = !itemViewing;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
} else if (e.type == SDL_MOUSEWHEEL) {
|
|
int dAmount = 0;
|
|
if (e.wheel.y > 0) {
|
|
dAmount = 1;
|
|
} else if (e.wheel.y < 0) {
|
|
dAmount = -1;
|
|
}
|
|
const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
|
|
if (keyboardState[SDL_SCANCODE_LCTRL] || keyboardState[SDL_SCANCODE_RCTRL]) {
|
|
// currentScale += dAmount / 10.0f;
|
|
// if (currentScale > 4) {
|
|
// currentScale = 4;
|
|
// } else if (currentScale < 0.5f) {
|
|
// currentScale = 0.5f;
|
|
// }
|
|
//setZoom(currentScale);
|
|
|
|
|
|
} else {
|
|
moveActivePlayerSlot(&mainPlayer, dAmount == -1,
|
|
!(keyboardState[SDL_SCANCODE_LSHIFT] || keyboardState[SDL_SCANCODE_RSHIFT]));
|
|
}
|
|
}
|
|
break;
|
|
case SCREEN_MENU:
|
|
break;
|
|
case SCREEN_FONTS:
|
|
break;
|
|
case SCREEN_ATLAS:
|
|
break;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void processMousePosition() {
|
|
mainPlayer.cursor.tileX = (mainPlayer.cursor.windowX + mainPlayer.rect.x) / TILE_SIZE - (DISPLAY_WIDTH / TILE_SIZE / 2);
|
|
mainPlayer.cursor.tileY = (mainPlayer.cursor.windowY + mainPlayer.rect.y) / TILE_SIZE - (DISPLAY_HEIGHT / TILE_SIZE / 2);
|
|
|
|
if (mainPlayer.cursor.tileX < 0) {
|
|
mainPlayer.cursor.tileX = 0;
|
|
}
|
|
if (mainPlayer.cursor.tileY < 0) {
|
|
mainPlayer.cursor.tileY = 0;
|
|
}
|
|
if (mainPlayer.cursor.tileX >= MAP_WIDTH) {
|
|
mainPlayer.cursor.tileX = MAP_WIDTH - 1;
|
|
}
|
|
if (mainPlayer.cursor.tileY >= MAP_HEIGHT) {
|
|
mainPlayer.cursor.tileY = MAP_HEIGHT - 1;
|
|
}
|
|
|
|
mainPlayer.cursor.prevTargetTile = mainPlayer.cursor.targetTile;
|
|
mainPlayer.cursor.targetTile = &tileMap[mainPlayer.cursor.tileY][mainPlayer.cursor.tileX];
|
|
mainPlayer.cursor.targetTileRect.x = mainPlayer.cursor.tileX * TILE_SIZE;
|
|
mainPlayer.cursor.targetTileRect.y = mainPlayer.cursor.tileY * TILE_SIZE;
|
|
mainPlayer.cursor.tileDiffX = mainPlayer.cursor.tileX - playerTileX;
|
|
mainPlayer.cursor.tileDiffY = mainPlayer.cursor.tileY - playerTileY;
|
|
mainPlayer.cursor.tileDiff = floorf(sqrtf(powf(mainPlayer.cursor.tileDiffX, 2) + powf(mainPlayer.cursor.tileDiffY, 2)));
|
|
mainPlayer.cursor.canReach = mainPlayer.cursor.tileDiff <= playerReach;
|
|
adjustRect(&mainPlayer.cursor.targetTileRect, mainPlayer.rect);
|
|
|
|
if (mainPlayer.mouseButtons & SDL_BUTTON_LMASK) {
|
|
if (mainPlayer.cursor.canReach && mainPlayer.cursor.targetTile->type == TYPE_AIR &&
|
|
mainPlayer.inventory.activeSlotIndex < tileTypeIndex) {
|
|
if (mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex] > 0) {
|
|
mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex]--;
|
|
mainPlayer.cursor.targetTile->type = mainPlayer.inventory.activeSlotIndex;
|
|
mainPlayer.cursor.targetTile->health = TileRegistry[mainPlayer.inventory.activeSlotIndex].maxHealth;
|
|
mainPlayer.cursor.targetTile->rect.x = mainPlayer.cursor.tileX;
|
|
mainPlayer.cursor.targetTile->rect.y = mainPlayer.cursor.tileY;
|
|
if (TileRegistry[mainPlayer.inventory.activeSlotIndex].needsTicks) {
|
|
mainPlayer.cursor.targetTile->neededUpdateIndex = add_tile(&neededUpdates,
|
|
mainPlayer.cursor.targetTile->rect);
|
|
}
|
|
mainPlayer.cursor.targetTile->direction = mainPlayer.cursor.direction;
|
|
}
|
|
} else if (mainPlayer.cursor.targetTile->type == mainPlayer.inventory.activeSlotIndex) {
|
|
mainPlayer.cursor.targetTile->direction = mainPlayer.cursor.direction;
|
|
}
|
|
}
|
|
if (mainPlayer.cursor.canReach && mainPlayer.mouseButtons & SDL_BUTTON_RMASK) {
|
|
int tileIndex = mainPlayer.cursor.targetTile->type;
|
|
uint16_t targetBreakTime = TileRegistry[tileIndex].breakTime;
|
|
if (targetBreakTime) {
|
|
if (mainPlayer.cursor.breakingProgress >= targetBreakTime) {
|
|
if (mainPlayer.cursor.targetTile->type < tileTypeIndex) {
|
|
mainPlayer.inventory.slotCounts[mainPlayer.cursor.targetTile->type]++;
|
|
}
|
|
for (int lane = 0; lane < 2; lane++) {
|
|
if (mainPlayer.cursor.targetTile->items[lane].type != 0) {
|
|
int itemType = mainPlayer.cursor.targetTile->items[lane].type;
|
|
if (itemType < itemRegistryIndex) {
|
|
mainPlayer.inventory.slotCounts[itemType]++;
|
|
}
|
|
mainPlayer.cursor.targetTile->items[lane].type = 0;
|
|
}
|
|
}
|
|
audioData.synthVoices[mainPlayer.cursor.targetTile->audioCh].volume = 0;
|
|
int neededIndex = mainPlayer.cursor.targetTile->neededUpdateIndex;
|
|
if (TileRegistry[mainPlayer.cursor.targetTile->type].needsTicks &&
|
|
neededUpdates.tiles[neededIndex].x == mainPlayer.cursor.targetTile->rect.x &&
|
|
neededUpdates.tiles[neededIndex].y == mainPlayer.cursor.targetTile->rect.y) {
|
|
remove_tile(&neededUpdates, neededIndex);
|
|
}
|
|
mainPlayer.cursor.targetTile->type = TYPE_AIR;
|
|
mainPlayer.cursor.breakingProgress = 0;
|
|
} else {
|
|
mainPlayer.cursor.breakingProgress++;
|
|
}
|
|
//printf("Player breaking %d\n", mainPlayer.cursor.breakingProgress);
|
|
}
|
|
|
|
} else {
|
|
mainPlayer.cursor.breakingProgress = 0;
|
|
}
|
|
if (mainPlayer.cursor.targetTile != mainPlayer.cursor.prevTargetTile) {
|
|
mainPlayer.cursor.breakingProgress = 0;
|
|
}
|
|
if (mainPlayer.mouseButtons & SDL_BUTTON_MMASK) {
|
|
if (mainPlayer.cursor.targetTile->type > 0) {
|
|
setActivePlayerSlot(&mainPlayer, mainPlayer.cursor.targetTile->type);
|
|
}
|
|
|
|
}
|
|
// Translate mouseRect coordinates to viewport space
|
|
}
|
|
|
|
void processKeyboardHeld() {
|
|
const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
|
|
|
|
int cameraSpeed = playerSpeed;
|
|
|
|
if (keyboardState[SDL_SCANCODE_LSHIFT] || keyboardState[SDL_SCANCODE_RSHIFT]) {
|
|
cameraSpeed *= 8;
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_LCTRL] || keyboardState[SDL_SCANCODE_RCTRL]) {
|
|
cameraSpeed /= 2;
|
|
}
|
|
|
|
if (keyboardState[SDL_SCANCODE_F8]) {
|
|
if (mainPlayer.cursor.targetTile->health) {
|
|
mainPlayer.cursor.targetTile->health--;
|
|
}
|
|
}
|
|
|
|
if (mainPlayer.cursor.breakingProgress == 0) {
|
|
SDL_Rect newRect = mainPlayer.rect;
|
|
|
|
if (keyboardState[SDL_SCANCODE_W]) {
|
|
newRect.y -= cameraSpeed;
|
|
if (newRect.y >= 0 && canMoveWithRadius(newRect)) {
|
|
mainPlayer.rect = newRect;
|
|
}
|
|
}
|
|
|
|
if (keyboardState[SDL_SCANCODE_S]) {
|
|
newRect = mainPlayer.rect;
|
|
newRect.y += cameraSpeed;
|
|
if (newRect.y + newRect.h <= MAP_HEIGHT * TILE_SIZE && canMoveWithRadius(newRect)) {
|
|
mainPlayer.rect = newRect;
|
|
}
|
|
}
|
|
|
|
if (keyboardState[SDL_SCANCODE_A]) {
|
|
newRect = mainPlayer.rect;
|
|
newRect.x -= cameraSpeed;
|
|
if (newRect.x >= 0 && canMoveWithRadius(newRect)) {
|
|
mainPlayer.rect = newRect;
|
|
}
|
|
}
|
|
|
|
if (keyboardState[SDL_SCANCODE_D]) {
|
|
newRect = mainPlayer.rect;
|
|
newRect.x += cameraSpeed;
|
|
if (newRect.x + newRect.w <= MAP_WIDTH * TILE_SIZE && canMoveWithRadius(newRect)) {
|
|
mainPlayer.rect = newRect;
|
|
}
|
|
}
|
|
|
|
|
|
// Update tileRect only after actual movement
|
|
mainPlayer.tileRect.x = mainPlayer.rect.x / TILE_SIZE;
|
|
mainPlayer.tileRect.y = mainPlayer.rect.y / TILE_SIZE;
|
|
}
|
|
|
|
|
|
if (keyboardState[SDL_SCANCODE_F]) {
|
|
for (int x = playerTileX - 2; x < playerTileX + 2; x++) {
|
|
if (x < 0) {
|
|
continue;
|
|
}
|
|
if (x >= MAP_WIDTH) {
|
|
continue;
|
|
}
|
|
for (int y = playerTileY - 2; y < playerTileY + 2; y++) {
|
|
if (y < 0) {
|
|
continue;
|
|
}
|
|
if (y >= MAP_HEIGHT) {
|
|
continue;
|
|
}
|
|
Tile *t = &tileMap[y][x];
|
|
if (t->type == TYPE_BELT) {
|
|
for (uint8_t lane = 0; lane < 2; lane++) {
|
|
ItemOnBelt *item = &t->items[lane];
|
|
if (item->type != 0) {
|
|
mainPlayer.inventory.slotCounts[item->type]++;
|
|
item->type = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (keyboardState[SDL_SCANCODE_Q]) {
|
|
if (mainPlayer.cursor.targetTile->type > 0 && mainPlayer.inventory.activeSlotIndex == 0) {
|
|
setActivePlayerSlot(&mainPlayer, mainPlayer.cursor.targetTile->type);
|
|
} else {
|
|
setActivePlayerSlot(&mainPlayer, 0);
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_E]) {
|
|
if (mainPlayer.cursor.targetTile->health < TileRegistry[mainPlayer.cursor.targetTile->type].maxHealth) {
|
|
mainPlayer.cursor.targetTile->health++;
|
|
}
|
|
for (int x = playerTileX - 2; x < playerTileX + 2; x++) {
|
|
if (x < 0) {
|
|
continue;
|
|
}
|
|
if (x >= MAP_WIDTH) {
|
|
continue;
|
|
}
|
|
for (int y = playerTileY - 2; y < playerTileY + 2; y++) {
|
|
if (y < 0) {
|
|
continue;
|
|
}
|
|
if (y >= MAP_HEIGHT) {
|
|
continue;
|
|
}
|
|
Tile *t = &tileMap[y][x];
|
|
if (t->health < TileRegistry[t->type].maxHealth) {
|
|
t->health++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_F9]) {
|
|
mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex]++;
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_Y]) {
|
|
if (mainPlayer.cursor.canReach && mainPlayer.cursor.targetTile->type == TYPE_BELT &&
|
|
mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex] > 0) {
|
|
for (uint8_t lane = 0; lane < 1; lane++) {
|
|
if (mainPlayer.cursor.targetTile->items[lane].type == 0) {
|
|
putItem(mainPlayer.cursor.tileX, mainPlayer.cursor.tileY, mainPlayer.inventory.activeSlotIndex, lane);
|
|
mainPlayer.inventory.slotCounts[mainPlayer.inventory.activeSlotIndex]--;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
unsigned int slot = 0;
|
|
if (keyboardState[SDL_SCANCODE_GRAVE]) {
|
|
slot = 1;
|
|
} else if (keyboardState[SDL_SCANCODE_1]) {
|
|
slot = 2;
|
|
} else if (keyboardState[SDL_SCANCODE_2]) {
|
|
slot = 3;
|
|
} else if (keyboardState[SDL_SCANCODE_3]) {
|
|
slot = 4;
|
|
} else if (keyboardState[SDL_SCANCODE_4]) {
|
|
slot = 5;
|
|
} else if (keyboardState[SDL_SCANCODE_5]) {
|
|
slot = 6;
|
|
} else if (keyboardState[SDL_SCANCODE_6]) {
|
|
slot = 7;
|
|
} else if (keyboardState[SDL_SCANCODE_7]) {
|
|
slot = 8;
|
|
} else if (keyboardState[SDL_SCANCODE_8]) {
|
|
slot = 9;
|
|
} else if (keyboardState[SDL_SCANCODE_9]) {
|
|
slot = 10;
|
|
} else if (keyboardState[SDL_SCANCODE_0]) {
|
|
slot = 11;
|
|
} else if (keyboardState[SDL_SCANCODE_MINUS]) {
|
|
slot = 12;
|
|
} else if (keyboardState[SDL_SCANCODE_EQUALS]) {
|
|
slot = 13;
|
|
}
|
|
if (slot > 0 && slot < sizeof(mainPlayer.inventory.hotKeySlots) / sizeof(mainPlayer.inventory.hotKeySlots[0])) {
|
|
setActivePlayerSlot(&mainPlayer, mainPlayer.inventory.hotKeySlots[slot]);
|
|
}
|
|
}
|
|
|
|
void basicUtil() {
|
|
mainPlayer.mouseButtons = SDL_GetMouseState(&mainPlayer.cursor.windowX, &mainPlayer.cursor.windowY);
|
|
|
|
mainPlayer.cursor.windowX = ((mainPlayer.cursor.windowX - viewport.x) * DISPLAY_WIDTH) / viewport.w;
|
|
mainPlayer.cursor.windowY = (mainPlayer.cursor.windowY - viewport.y) * DISPLAY_HEIGHT / viewport.h;
|
|
SDL_Event e;
|
|
while (SDL_PollEvent(&e)) {
|
|
running = processEvent(e);
|
|
}
|
|
}
|
|
|
|
int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[]) {
|
|
int status = init();
|
|
if (status) {
|
|
return status;
|
|
}
|
|
|
|
//Hack to get window to stay up
|
|
Uint64 start;
|
|
Uint64 end;
|
|
while (running) {
|
|
start = SDL_GetTicks64();
|
|
|
|
basicUtil();
|
|
SDL_SetRenderDrawColor(mainRenderer, 32, 32, 32, 255);
|
|
SDL_RenderClear(mainRenderer);
|
|
switch (screenType) {
|
|
|
|
case SCREEN_MENU:
|
|
renderText(mainRenderer, fonts[0], GAME_NAME,
|
|
DISPLAY_WIDTH / 2 - (strlen(GAME_NAME) * fonts[0].size / 2),
|
|
DISPLAY_HEIGHT / 8 - fonts[0].size);
|
|
|
|
break;
|
|
case SCREEN_FONTS:
|
|
unsigned int ix = 0;
|
|
for (unsigned char i = 0; i < fontCount; i++) {
|
|
SDL_Rect tmpRectFont = fonts[i].atlasRect;
|
|
tmpRectFont.x += ix;
|
|
SDL_Rect tmpRectFontOut = tmpRectFont;
|
|
if (DISPLAY_HEIGHT < 1000) {
|
|
tmpRectFontOut.w /= 2;
|
|
tmpRectFontOut.h /= 2;
|
|
}
|
|
SDL_RenderCopy(mainRenderer, fonts[i].atlas, &fonts[i].atlasRect, &tmpRectFontOut);
|
|
ix += fonts[i].atlasRect.w / 2;
|
|
}
|
|
break;
|
|
case SCREEN_ATLAS:
|
|
lateInit();
|
|
SDL_Rect rect2;
|
|
rect2.x = 0;
|
|
rect2.y = 0;
|
|
rect2.w = 2048;
|
|
rect2.h = 1024;
|
|
SDL_Rect tmpRectFontOut = rect2;
|
|
if (!largeScreen) {
|
|
tmpRectFontOut.w /= 2;
|
|
tmpRectFontOut.h /= 2;
|
|
}
|
|
SDL_RenderCopy(mainRenderer, atlasTexture, &rect2, &tmpRectFontOut);
|
|
rect2.x = 0;
|
|
rect2.y = 1024;
|
|
rect2.w = 2048;
|
|
rect2.h = 1024;
|
|
tmpRectFontOut = rect2;
|
|
tmpRectFontOut.x = 1024;
|
|
if (!largeScreen) {
|
|
tmpRectFontOut.x /= 2;
|
|
tmpRectFontOut.w /= 2;
|
|
tmpRectFontOut.h /= 2;
|
|
}
|
|
SDL_RenderCopy(mainRenderer, atlasTexture, &rect2, &tmpRectFontOut);
|
|
break;
|
|
case SCREEN_CREDITS:
|
|
renderText(mainRenderer, fonts[0], GAME_NAME,
|
|
DISPLAY_WIDTH / 2 - (strlen(GAME_NAME) * fonts[0].size / 2),
|
|
DISPLAY_HEIGHT / 8 - fonts[0].size);
|
|
char *creditsString = "Code by BRNSystems\nArt by Simi11\nMalo sa to volat Minidustry\nale Simi11 mal iny nazor\n(a ikonu)\nMade for Mr. Kovacev";
|
|
renderText(mainRenderer, fonts[0], creditsString,
|
|
0,
|
|
DISPLAY_HEIGHT / 4 - fonts[0].size);
|
|
break;
|
|
|
|
case SCREEN_GAME:
|
|
lateInit();
|
|
processMousePosition();
|
|
processKeyboardHeld();
|
|
|
|
|
|
updateWaveLogic(&waveInfo);
|
|
updateItems();
|
|
updateEntities(&mainPlayer);
|
|
updatePlayer(&mainPlayer);
|
|
updateTiles();
|
|
animationStep++;
|
|
status = render();
|
|
if (status) {
|
|
return status;
|
|
}
|
|
break;
|
|
}
|
|
renderButtons(mainRenderer, mainPlayer);
|
|
|
|
SDL_RenderPresent(mainRenderer);
|
|
frames++;
|
|
if (!(frames % 60)) {
|
|
cursor = !cursor;
|
|
}
|
|
|
|
end = SDL_GetTicks64();
|
|
const unsigned long timeNeeded = end - start;
|
|
if (timeNeeded < delayNeeded) {
|
|
SDL_Delay(delayNeeded - timeNeeded);
|
|
}
|
|
|
|
}
|
|
|
|
saveGameState(autosaveName, &mainPlayer);
|
|
|
|
for (uint8_t i = 0; i < fontCount; i++) {
|
|
destroyFont(&fonts[i]);
|
|
}
|
|
puts(SDL_GetError());
|
|
if (mainRenderer) SDL_DestroyRenderer(mainRenderer);
|
|
if (window) SDL_DestroyWindow(window);
|
|
if (glContext) SDL_GL_DeleteContext(glContext);
|
|
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
}
|