Nimbin2git.christianimmanuel.de / Games / Entropie / main.c

Entropie git · main

git clone https://git.christianimmanuel.de/games/Entropie.gitwget https://git.christianimmanuel.de/games/Entropie/archive/Entropie.tar.gz
main.c 5.9 KB · 200 lines raw
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <float.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_init.h>
#include <SDL3_ttf/SDL_ttf.h>
#include "character.h"
#include "render.h"
#include "map.h"
#include "camera.h"

#define MOVEMENT_SPEED 100.0f

TileType** map;
MapObjectType*** map_objects;
unsigned char** map_objects_count;
int MAP_WIDTH  = 0;
int MAP_HEIGHT = 0;
const int TILE_SIZE = 32;

int characters_size;
Character* characters;
Character* character;
Object*    object_first;



int main() {
    Camera camera;
    camera.view_w = 960;
    camera.view_h = 620;

    if (!SDL_Init(SDL_INIT_VIDEO)) {
        fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
        return 1;
    }

    if (!TTF_Init()) {
        fprintf(stderr, "TTF_Init Error..\n");
        SDL_Quit();
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("Entropie",
                                          camera.view_w, camera.view_h,
                                          0);

    // Initialize the camera to be centered on the window initially
    camera.center_x = camera.view_w / 2;
    camera.center_y = camera.view_h / 2;

    if (!window) {
        fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL);
    if (!renderer) {
        fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    TTF_Font* font = TTF_OpenFont("font.ttf", 16);
    if (!font) {
        fprintf(stderr, "TTF_OpenFont Error..\n");
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    srand(42);


    object_first = Object_create_pos(FLT_MIN,FLT_MIN,0,0);

    characters = Character_creates(object_first, &characters_size);
    int character_active_id = 0;
    character = &characters[character_active_id];
    printf("\n\n### %d %d %d\n", character->color.r, character->color.g, character->color.b);
    printf("\n\n### %f %f %f %f\n", character->object->x, character->object->y, character->object->w, character->object->h);

    // Load the map and assign character positions
    if (Map_load("hof.map", characters, characters_size, object_first) != 0) {
        fprintf(stderr, "Failed to load map\n");
        return 1;
    }
    printf("MAP: %d %d\n", MAP_WIDTH, MAP_HEIGHT);
    printf("\n\n### %f %f %f %f\n", character->object->x, character->object->y, character->object->w, character->object->h);



    Object* obj = object_first;
    while (obj->next) {
        object_log(obj->next);
        obj = obj->next;
    }


    int running = 1;
    Uint32 last_time = SDL_GetTicks();

    while (running) {
        Uint32 current_time = SDL_GetTicks();
        float delta_time = (current_time - last_time) / 1000.0f;
        last_time = current_time;

        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_EVENT_QUIT) {
                running = 0;
            }
            if (event.type == SDL_EVENT_KEY_DOWN) {
                SDL_Keycode keycode = SDL_GetKeyFromScancode(event.key.scancode, event.key.mod, false);
                if (keycode == 'n' || keycode == 'p') {
                    if (keycode == 'n'  &&
                            ++character_active_id >= characters_size)
                        character_active_id = 0;
                    if (keycode == 'p'  &&
                            --character_active_id < 0)
                        character_active_id = characters_size-1;
                    character = &characters[character_active_id];
                }
            }
        }

        // Update the internal input state
        SDL_PumpEvents();

        // Get the current keyboard state
        const bool *keyboard_state = SDL_GetKeyboardState(NULL);

        // Movement vector initialization
        float move_x = 0.0f;
        float move_y = 0.0f;

        // Check key states for movement
        if (keyboard_state[SDL_SCANCODE_W]) move_y -= 1.0f;  // Move up
        if (keyboard_state[SDL_SCANCODE_S]) move_y += 1.0f;  // Move down
        if (keyboard_state[SDL_SCANCODE_A]) move_x -= 1.0f;  // Move left
        if (keyboard_state[SDL_SCANCODE_D]) move_x += 1.0f;  // Move right

        // Apply scaling factor for diagonal movement
        if (move_x != 0.0f && move_y != 0.0f) {
            move_x *= 0.707f;
            move_y *= 0.707f;
        }

        // Calculate the new position
        float new_x = character->object->x + move_x * MOVEMENT_SPEED * delta_time;
        float new_y = character->object->y + move_y * MOVEMENT_SPEED * delta_time;

        // Check collisions
        if (Map_isPassable(new_x, character->object->y, character->object->w, character->object->h)) {
            character->object->x = new_x;
        }
        if (Map_isPassable(character->object->x, new_y, character->object->w, character->object->h)) {
            character->object->y = new_y;
        }

        // Center camera
        camera.center_x = character->object->x;
        camera.center_y = character->object->y;
        Object* ro = character->object;
        while (ro->prev && Object_is_visible(ro, &camera))
            ro = ro->prev;
        if (ro->prev)
            ro = ro->next;
        camera.render_object = (void*)ro;
        setCamera(&camera);



        // Clear screen
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);



        // Render stuff
        Render_map(renderer, camera, character->object);
        Render_characterInfo(renderer, font, character);
        //Render_rect(renderer, camera.center_x - 2, camera.center_y - 2, 4, 4, 255, 255, 0, 255);

        SDL_RenderPresent(renderer);
    }

    Character_free(characters, characters_size);
    Map_free();
    Objects_free(object_first);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}