Some progress
This commit is contained in:
160
main.c
160
main.c
@@ -9,19 +9,16 @@
|
||||
#include "stdlib.h"
|
||||
#include "player/player.h"
|
||||
|
||||
Player player;
|
||||
|
||||
float currentScale = 1;
|
||||
|
||||
//Screen dimension constants
|
||||
const int SCREEN_WIDTH = DISPLAY_WIDTH;
|
||||
const int SCREEN_HEIGHT = DISPLAY_HEIGHT;
|
||||
#define SCREEN_WIDTH DISPLAY_WIDTH / currentScale
|
||||
#define SCREEN_HEIGHT DISPLAY_HEIGHT/ currentScale
|
||||
const int targetFPS = 60;
|
||||
const int delayNeeded = 1000 / targetFPS;
|
||||
|
||||
//The window we'll be rendering to
|
||||
SDL_Window *window = NULL;
|
||||
volatile bool running = true;
|
||||
|
||||
//The surface contained by the window
|
||||
SDL_Renderer *renderer = NULL;
|
||||
|
||||
#define biggerFont fonts[0]
|
||||
#define smallFont fonts[1]
|
||||
#define smallerFont fonts[2]
|
||||
@@ -30,6 +27,7 @@ SDL_Renderer *renderer = NULL;
|
||||
|
||||
unsigned long frames = 0;
|
||||
bool cursor = true;
|
||||
|
||||
void msleep(unsigned int milliseconds) {
|
||||
struct timespec ts;
|
||||
ts.tv_sec = milliseconds / 1000;
|
||||
@@ -42,11 +40,13 @@ 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;
|
||||
@@ -61,6 +61,7 @@ int init() {
|
||||
//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;
|
||||
@@ -72,8 +73,8 @@ int init() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
loadItems(renderer);
|
||||
loadTiles(renderer);
|
||||
loadItems(renderer);
|
||||
// Create OpenGL context
|
||||
SDL_GLContext glContext = SDL_GL_CreateContext(window);
|
||||
if (!glContext) {
|
||||
@@ -102,7 +103,6 @@ int init() {
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_SetRenderTarget(renderer, NULL);
|
||||
|
||||
SDL_Rect viewport = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
SDL_RenderSetViewport(renderer, &viewport);
|
||||
@@ -114,6 +114,8 @@ int init() {
|
||||
smallestFont = prepText(renderer, 4, "assets/PublicPixel.ttf", 255, 255, 255, 255);
|
||||
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
|
||||
initPlayer(&player);
|
||||
|
||||
generateTestMap();
|
||||
|
||||
return 0;
|
||||
@@ -132,6 +134,8 @@ int render() {
|
||||
|
||||
renderAllBelts(renderer);
|
||||
|
||||
renderPlayer(&player);
|
||||
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
frames++;
|
||||
@@ -162,28 +166,127 @@ int processEvent(SDL_Event e) {
|
||||
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_MOUSEBUTTONDOWN) {
|
||||
SDL_Rect viewport;
|
||||
SDL_RenderGetViewport(renderer, &viewport);
|
||||
} 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);
|
||||
|
||||
SDL_Rect mouset;
|
||||
mouset.w = 1;
|
||||
mouset.h = 1;
|
||||
SDL_GetMouseState(&mouset.x, &mouset.y);
|
||||
// Translate mouse coordinates to viewport space
|
||||
SDL_Rect mouse;
|
||||
mouse.w = 1;
|
||||
mouse.h = 1;
|
||||
mouse.x = ((mouset.x - viewport.x) * SCREEN_WIDTH) / viewport.w;
|
||||
mouse.y = (mouset.y - viewport.y) * SCREEN_HEIGHT / viewport.h;
|
||||
|
||||
} 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) {
|
||||
@@ -191,8 +294,8 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
|
||||
}
|
||||
|
||||
uint8_t type = 0;
|
||||
for (int x = 142; x < 154; x+=3) {
|
||||
for(int y = 80; y < 94; y+=3) {
|
||||
for (int x = 142; x < 154; x += 3) {
|
||||
for (int y = 80; y < 94; y += 3) {
|
||||
putItem(x, y, type++ % ITEMREGISTRY_SIZE, 0, 0);
|
||||
}
|
||||
}
|
||||
@@ -205,6 +308,9 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
|
||||
while (running) {
|
||||
start = SDL_GetTicks64();
|
||||
|
||||
processMousePosition();
|
||||
processKeyboardHeld();
|
||||
|
||||
while (SDL_PollEvent(&e)) {
|
||||
running = processEvent(e);
|
||||
}
|
||||
|
Reference in New Issue
Block a user