341 lines
10 KiB
C
341 lines
10 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 "tiles/belt.h"
|
|
#include "items/item.h"
|
|
#include "stdlib.h"
|
|
#include "player/player.h"
|
|
|
|
Player player;
|
|
|
|
float currentScale = 1;
|
|
|
|
//Screen dimension constants
|
|
#define SCREEN_WIDTH DISPLAY_WIDTH / currentScale
|
|
#define SCREEN_HEIGHT DISPLAY_HEIGHT/ currentScale
|
|
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]
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
int init() {
|
|
//Initialize SDL
|
|
|
|
|
|
|
|
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, SCREEN_WIDTH,
|
|
SCREEN_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
|
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
|
if (renderer == NULL) {
|
|
printf("Renderer could not be created SDL_Error: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
loadTiles(renderer);
|
|
loadItems(renderer);
|
|
// Create OpenGL context
|
|
SDL_GLContext 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(renderer, 0, 0, 0, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_Rect viewport = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
|
|
SDL_RenderSetViewport(renderer, &viewport);
|
|
|
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
|
biggerFont = prepText(renderer, 16, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
|
smallFont = prepText(renderer, 12, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
|
smallerFont = prepText(renderer, 8, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
|
smallestFont = prepText(renderer, 4, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
|
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
|
|
initPlayer(&player);
|
|
|
|
generateTestMap();
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int render() {
|
|
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_Rect rect2;
|
|
rect2.x = 20;
|
|
rect2.y = 20;
|
|
rect2.w = 0;
|
|
rect2.h = 0;
|
|
|
|
renderAllBelts(renderer);
|
|
|
|
renderPlayer(&player);
|
|
|
|
|
|
SDL_RenderPresent(renderer);
|
|
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(renderer, &viewport);
|
|
} else if (e.type == SDL_KEYDOWN) {
|
|
int keySym = e.key.keysym.sym;
|
|
int keyMod = e.key.keysym.mod;
|
|
cursor = true;
|
|
switch (keySym) {
|
|
case SDLK_q:
|
|
laneTarget = laneTarget == 1 ? 0 : 1;
|
|
break;
|
|
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;
|
|
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 {
|
|
setActivePlayerSlot(&player, player.inventory.activeSlotIndex + dAmount);
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void processMousePosition() {
|
|
SDL_Rect viewport;
|
|
SDL_RenderGetViewport(renderer, &viewport);
|
|
|
|
uint32_t mouseButtons = SDL_GetMouseState(&player.cursor.windowX, &player.cursor.windowY);
|
|
if (mouseButtons & SDL_BUTTON_LMASK) {
|
|
if(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 + 1;
|
|
player.cursor.targetTile->direction = player.cursor.direction;
|
|
}
|
|
}
|
|
} else if (mouseButtons & SDL_BUTTON_RMASK) {
|
|
if (player.cursor.targetTile != player.cursor.prevTargetTile) {
|
|
player.cursor.breakingProgress = 0;
|
|
}
|
|
int tileIndex = player.cursor.targetTile->type - 1;
|
|
uint16_t targetBreakTime = TileRegistry[tileIndex].breakTime;
|
|
if (targetBreakTime) {
|
|
if (player.cursor.breakingProgress >= targetBreakTime) {
|
|
if (player.cursor.targetTile->type - 1 < tileTypeIndex) {
|
|
player.inventory.slotCounts[player.cursor.targetTile->type - 1]++;
|
|
}
|
|
player.cursor.targetTile->type = TYPE_AIR;
|
|
player.cursor.breakingProgress = 0;
|
|
} else {
|
|
player.cursor.breakingProgress++;
|
|
}
|
|
printf("Player breaking %d\n", player.cursor.breakingProgress);
|
|
}
|
|
|
|
} else if (mouseButtons & SDL_BUTTON_MMASK) {
|
|
|
|
}
|
|
// Translate mouseRect coordinates to viewport space
|
|
|
|
player.cursor.windowX = ((player.cursor.windowX - viewport.x) * SCREEN_WIDTH) / viewport.w;
|
|
player.cursor.windowY = (player.cursor.windowY - viewport.y) * SCREEN_HEIGHT / viewport.h;
|
|
player.cursor.tileX = player.cursor.windowX / TILE_SIZE + (playerX / TILE_SIZE) - (SCREEN_WIDTH / TILE_SIZE / 2);
|
|
player.cursor.tileY = player.cursor.windowY / TILE_SIZE + (playerY / TILE_SIZE) - (SCREEN_HEIGHT / TILE_SIZE / 2);
|
|
player.cursor.prevTargetTile = player.cursor.targetTile;
|
|
player.cursor.targetTile = &tileMap[player.cursor.tileY][player.cursor.tileX];
|
|
}
|
|
|
|
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 (keyboardState[SDL_SCANCODE_W]) {
|
|
// Example: move up
|
|
playerY -= cameraSpeed;
|
|
if (playerY < (SCREEN_HEIGHT / 2)) {
|
|
playerY = (SCREEN_HEIGHT / 2);
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_S]) {
|
|
playerY += cameraSpeed;
|
|
if (playerY > (MAP_HEIGHT * TILE_SIZE) - (SCREEN_HEIGHT / 2)) {
|
|
playerY = (MAP_HEIGHT * TILE_SIZE) - (SCREEN_HEIGHT / 2);
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_A]) {
|
|
playerX -= cameraSpeed;
|
|
if (playerX < (SCREEN_WIDTH / 2)) {
|
|
playerX = (SCREEN_WIDTH / 2);
|
|
}
|
|
}
|
|
if (keyboardState[SDL_SCANCODE_D]) {
|
|
playerX += cameraSpeed;
|
|
if (playerX > (MAP_WIDTH * TILE_SIZE) - (SCREEN_WIDTH / 2)) {
|
|
playerX = (MAP_WIDTH * TILE_SIZE) - (SCREEN_WIDTH / 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[]) {
|
|
int status = init();
|
|
if (status) {
|
|
return status;
|
|
}
|
|
|
|
uint8_t type = 0;
|
|
for (int x = 142; x < 154; x += 3) {
|
|
for (int y = 80; y < 94; y += 3) {
|
|
putItem(x, y, type++ % ITEMREGISTRY_SIZE, 0, 0);
|
|
}
|
|
}
|
|
|
|
|
|
//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();
|
|
status = render();
|
|
if (status) {
|
|
return status;
|
|
}
|
|
|
|
end = SDL_GetTicks64();
|
|
const unsigned long timeNeeded = end - start;
|
|
if (timeNeeded < delayNeeded) {
|
|
SDL_Delay(delayNeeded - timeNeeded);
|
|
}
|
|
}
|
|
|
|
for (uint8_t i = 0; i < fontCount; i++) {
|
|
destroyFont(&fonts[i]);
|
|
}
|
|
puts(SDL_GetError());
|
|
if (renderer) SDL_DestroyRenderer(renderer);
|
|
if (window) SDL_DestroyWindow(window);
|
|
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
} |