488 lines
16 KiB
C
488 lines
16 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"
|
|
|
|
typedef struct {
|
|
Player player;
|
|
Tile tileMap[MAP_HEIGHT][MAP_WIDTH];
|
|
} GameState;
|
|
GameState gameState;
|
|
|
|
void loadGameState(char *filename, Player *plr) {
|
|
printf("hello from load\n");
|
|
fflush(stdout);
|
|
FILE *gameSave = fopen(filename, "rb");
|
|
if (gameSave) {
|
|
fread(&gameState, sizeof(gameState), 1, gameSave);
|
|
fclose(gameSave);
|
|
memcpy(tileMap, gameState.tileMap, sizeof(tileMap));
|
|
memcpy(plr, &gameState.player, sizeof(gameState.player));
|
|
}
|
|
}
|
|
|
|
void saveGameState(char *filename, Player *plr) {
|
|
memcpy(&gameState.player, plr, sizeof(gameState.player));
|
|
memcpy(gameState.tileMap, tileMap, sizeof(gameState.tileMap));
|
|
|
|
FILE *gameSave = fopen(filename, "wb");
|
|
if (!gameSave) {
|
|
perror("Failed to open file for saving");
|
|
return;
|
|
}
|
|
|
|
fwrite(&gameState, sizeof(gameState), 1, gameSave);
|
|
fclose(gameSave);
|
|
}
|
|
|
|
Player player;
|
|
|
|
//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]
|
|
|
|
const char *autosaveName = "autosave.dat";
|
|
|
|
|
|
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
|
|
|
|
|
|
screenRect.x = 0;
|
|
screenRect.y = 0;
|
|
screenRect.w = DISPLAY_WIDTH;
|
|
screenRect.h = DISPLAY_HEIGHT;
|
|
|
|
srand(0);
|
|
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, NULL);
|
|
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "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("Factory game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DISPLAY_WIDTH,
|
|
DISPLAY_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
|
|
|
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;
|
|
}
|
|
|
|
loadTiles(mainRenderer);
|
|
loadItems(mainRenderer);
|
|
// 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
|
|
|
|
SDL_AudioSpec spec = {0};
|
|
spec.freq = SAMPLE_RATE;
|
|
spec.format = AUDIO_F32SYS;
|
|
spec.channels = 1;
|
|
spec.samples = 4096;
|
|
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();
|
|
}
|
|
|
|
SDL_PauseAudioDevice(dev, 0);
|
|
|
|
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
|
|
SDL_RenderClear(mainRenderer);
|
|
|
|
SDL_Rect viewport = {0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT};
|
|
SDL_RenderSetViewport(mainRenderer, &viewport);
|
|
|
|
SDL_SetRenderDrawBlendMode(mainRenderer, SDL_BLENDMODE_BLEND);
|
|
biggerFont = prepText(mainRenderer, 16, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
|
smallFont = prepText(mainRenderer, 12, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
|
smallerFont = prepText(mainRenderer, 8, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
|
smallestFont = prepText(mainRenderer, 4, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
|
SDL_RenderSetLogicalSize(mainRenderer, DISPLAY_WIDTH, DISPLAY_HEIGHT);
|
|
|
|
initPlayer(&player);
|
|
|
|
initTiles();
|
|
|
|
//generateTestMap();
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int render() {
|
|
SDL_SetRenderDrawColor(mainRenderer, 32, 32, 32, 255);
|
|
SDL_RenderClear(mainRenderer);
|
|
|
|
SDL_Rect rect2;
|
|
rect2.x = 20;
|
|
rect2.y = 20;
|
|
rect2.w = 0;
|
|
rect2.h = 0;
|
|
|
|
renderAllTiles(mainRenderer, player.rect);
|
|
|
|
SDL_RenderCopy(mainRenderer, tilesTexture, &screenRect, &screenRect);
|
|
SDL_RenderCopy(mainRenderer, itemsTexture, &screenRect, &screenRect);
|
|
SDL_RenderCopy(mainRenderer, entityTexture, &screenRect, &screenRect);
|
|
SDL_RenderCopy(mainRenderer, hudTexture, &screenRect, &screenRect);
|
|
|
|
renderPlayer(&player);
|
|
|
|
|
|
SDL_RenderPresent(mainRenderer);
|
|
frames++;
|
|
if (!(frames % 60)) {
|
|
cursor = !cursor;
|
|
}
|
|
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;
|
|
SDL_Rect viewport = {0, 0, newWidth, newHeight};
|
|
SDL_RenderSetViewport(mainRenderer, &viewport);
|
|
} else 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 (player.cursor.canReach && player.cursor.targetTile->type == TYPE_BELT) {
|
|
player.cursor.direction = player.cursor.targetTile->direction;
|
|
}
|
|
player.cursor.direction = (player.cursor.direction + 2) % ORIENT_DIRECTION_COUNT;
|
|
if (player.cursor.canReach && player.cursor.targetTile->type == TYPE_BELT) {
|
|
player.cursor.targetTile->direction = player.cursor.direction;
|
|
}
|
|
break;
|
|
case SDLK_u:
|
|
laneTarget = !laneTarget;
|
|
break;
|
|
case SDLK_F3:
|
|
debugMode = !debugMode;
|
|
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(&player, dAmount == -1,
|
|
!(keyboardState[SDL_SCANCODE_LSHIFT] || keyboardState[SDL_SCANCODE_RSHIFT]));
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void processMousePosition() {
|
|
SDL_Rect viewport;
|
|
SDL_RenderGetViewport(mainRenderer, &viewport);
|
|
|
|
uint32_t mouseButtons = SDL_GetMouseState(&player.cursor.windowX, &player.cursor.windowY);
|
|
if (mouseButtons & SDL_BUTTON_LMASK) {
|
|
if (player.cursor.canReach && player.cursor.targetTile->type == TYPE_AIR &&
|
|
player.inventory.activeSlotIndex < tileTypeIndex) {
|
|
if (player.inventory.slotCounts[player.inventory.activeSlotIndex] > 0) {
|
|
player.inventory.slotCounts[player.inventory.activeSlotIndex]--;
|
|
player.cursor.targetTile->type = player.inventory.activeSlotIndex;
|
|
player.cursor.targetTile->direction = player.cursor.direction;
|
|
}
|
|
} else if (player.cursor.targetTile->type == player.inventory.activeSlotIndex) {
|
|
player.cursor.targetTile->direction = player.cursor.direction;
|
|
}
|
|
}
|
|
if (player.cursor.canReach && mouseButtons & SDL_BUTTON_RMASK) {
|
|
int tileIndex = player.cursor.targetTile->type;
|
|
uint16_t targetBreakTime = TileRegistry[tileIndex].breakTime;
|
|
if (targetBreakTime) {
|
|
if (player.cursor.breakingProgress >= targetBreakTime) {
|
|
if (player.cursor.targetTile->type < tileTypeIndex) {
|
|
player.inventory.slotCounts[player.cursor.targetTile->type]++;
|
|
}
|
|
if (player.cursor.targetTile->type == TYPE_BELT) {
|
|
for (int lane = 0; lane < 2; lane++) {
|
|
if (player.cursor.targetTile->items[lane].type != 0) {
|
|
int itemType = player.cursor.targetTile->items[lane].type;
|
|
if (itemType < itemRegistryIndex) {
|
|
player.inventory.slotCounts[itemType]++;
|
|
}
|
|
player.cursor.targetTile->items[lane].type = 0;
|
|
}
|
|
}
|
|
}
|
|
player.cursor.targetTile->type = TYPE_AIR;
|
|
player.cursor.breakingProgress = 0;
|
|
} else {
|
|
player.cursor.breakingProgress++;
|
|
}
|
|
//printf("Player breaking %d\n", player.cursor.breakingProgress);
|
|
}
|
|
|
|
} else {
|
|
player.cursor.breakingProgress = 0;
|
|
}
|
|
if (player.cursor.targetTile != player.cursor.prevTargetTile) {
|
|
player.cursor.breakingProgress = 0;
|
|
}
|
|
if (mouseButtons & SDL_BUTTON_MMASK) {
|
|
if (player.cursor.targetTile->type > 0) {
|
|
setActivePlayerSlot(&player, player.cursor.targetTile->type);
|
|
}
|
|
|
|
}
|
|
// Translate mouseRect coordinates to viewport space
|
|
|
|
player.cursor.windowX = ((player.cursor.windowX - viewport.x) * DISPLAY_WIDTH) / viewport.w;
|
|
player.cursor.windowY = (player.cursor.windowY - viewport.y) * DISPLAY_HEIGHT / viewport.h;
|
|
player.cursor.tileX = (player.cursor.windowX + player.rect.x) / TILE_SIZE - (DISPLAY_WIDTH / TILE_SIZE / 2);
|
|
player.cursor.tileY = (player.cursor.windowY + player.rect.y) / TILE_SIZE - (DISPLAY_HEIGHT / TILE_SIZE / 2);
|
|
player.cursor.prevTargetTile = player.cursor.targetTile;
|
|
player.cursor.targetTile = &tileMap[player.cursor.tileY][player.cursor.tileX];
|
|
|
|
player.cursor.targetTileRect.x = player.cursor.tileX * TILE_SIZE;
|
|
player.cursor.targetTileRect.y = player.cursor.tileY * TILE_SIZE;
|
|
player.cursor.tileDiffX = player.cursor.tileX - playerTileX;
|
|
player.cursor.tileDiffY = player.cursor.tileY - playerTileY;
|
|
player.cursor.tileDiff = floorf(sqrtf(powf(player.cursor.tileDiffX, 2) + powf(player.cursor.tileDiffY, 2)));
|
|
player.cursor.canReach = player.cursor.tileDiff <= playerReach;
|
|
adjustRect(&player.cursor.targetTileRect, player.rect);
|
|
}
|
|
|
|
void processKeyboardHeld() {
|
|
const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
|
|
|
|
int cameraSpeed = playerSpeed;
|
|
|
|
if (keyboardState[SDL_SCANCODE_LSHIFT] || keyboardState[SDL_SCANCODE_RSHIFT]) {
|
|
cameraSpeed *= 2;
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_LCTRL] || keyboardState[SDL_SCANCODE_RCTRL]) {
|
|
cameraSpeed /= 2;
|
|
}
|
|
|
|
if (player.cursor.breakingProgress == 0) {
|
|
if (keyboardState[SDL_SCANCODE_W]) {
|
|
// Example: move up
|
|
player.rect.y -= cameraSpeed;
|
|
if (player.rect.y < (DISPLAY_HEIGHT / 2)) {
|
|
player.rect.y = (DISPLAY_HEIGHT / 2);
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_S]) {
|
|
player.rect.y += cameraSpeed;
|
|
if (player.rect.y > (MAP_HEIGHT * TILE_SIZE) - (DISPLAY_HEIGHT / 2)) {
|
|
player.rect.y = (MAP_HEIGHT * TILE_SIZE) - (DISPLAY_HEIGHT / 2);
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_A]) {
|
|
player.rect.x -= cameraSpeed;
|
|
if (player.rect.x < (DISPLAY_WIDTH / 2)) {
|
|
player.rect.x = (DISPLAY_WIDTH / 2);
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_D]) {
|
|
player.rect.x += cameraSpeed;
|
|
if (player.rect.x > (MAP_WIDTH * TILE_SIZE) - (DISPLAY_WIDTH / 2)) {
|
|
player.rect.x = (MAP_WIDTH * TILE_SIZE) - (DISPLAY_WIDTH / 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (keyboardState[SDL_SCANCODE_F]) {
|
|
for (int x = player.rect.x - 1; x < player.rect.x + 1; player.rect.x++) {
|
|
if (x < 0) {
|
|
continue;
|
|
}
|
|
for (int y = player.rect.y - 1; y < player.rect.y + 1; player.rect.y++) {
|
|
if (y < 0) {
|
|
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) {
|
|
player.inventory.slotCounts[item->type]++;
|
|
item->type = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (keyboardState[SDL_SCANCODE_Q]) {
|
|
if (player.cursor.targetTile->type > 0) {
|
|
setActivePlayerSlot(&player, player.cursor.targetTile->type);
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_Y]) {
|
|
if (player.cursor.canReach && player.cursor.targetTile->type == TYPE_BELT &&
|
|
player.inventory.slotCounts[player.inventory.activeSlotIndex] > 0) {
|
|
for (uint8_t lane = 0; lane < 1; lane++) {
|
|
if (player.cursor.targetTile->items[lane].type == 0) {
|
|
putItem(player.cursor.tileX, player.cursor.tileY, player.inventory.activeSlotIndex, lane);
|
|
player.inventory.slotCounts[player.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 < itemRegistryIndex) {
|
|
setActivePlayerSlot(&player, slot);
|
|
}
|
|
}
|
|
|
|
int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[]) {
|
|
int status = init();
|
|
if (status) {
|
|
return status;
|
|
}
|
|
loadGameState(autosaveName, &player);
|
|
|
|
|
|
//Hack to get window to stay up
|
|
SDL_Event e;
|
|
Uint64 start;
|
|
Uint64 end;
|
|
while (running) {
|
|
start = SDL_GetTicks64();
|
|
|
|
processMousePosition();
|
|
processKeyboardHeld();
|
|
|
|
while (SDL_PollEvent(&e)) {
|
|
running = processEvent(e);
|
|
}
|
|
|
|
updateItems();
|
|
updatePlayer(&player);
|
|
status = render();
|
|
if (status) {
|
|
return status;
|
|
}
|
|
|
|
end = SDL_GetTicks64();
|
|
const unsigned long timeNeeded = end - start;
|
|
if (timeNeeded < delayNeeded) {
|
|
SDL_Delay(delayNeeded - timeNeeded);
|
|
}
|
|
}
|
|
|
|
saveGameState(autosaveName, &player);
|
|
|
|
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;
|
|
} |