more component stuff

This commit is contained in:
Josh Lyon
2025-08-13 22:21:37 -06:00
parent f6de0ed9f3
commit 7259d9307b
8 changed files with 151 additions and 43 deletions

View File

@@ -9,6 +9,8 @@
including commercial applications, and to alter it and redistribute it
freely.
*/
#include "GameState.h"
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_rect.h>
#include <SDL3/SDL_render.h>
#include <string>
@@ -20,66 +22,54 @@
#include <memory>
#include "components/PlayerGraphics.h"
#include "components/PlayerInput.h"
#include "Graphics.h"
#include "GameObject.h"
static SDL_Window *window = nullptr;
static SDL_Renderer *renderer = nullptr;
GameObject player(std::make_unique<PlayerGraphics>());
Graphics graphics;
GameObject player(
std::make_unique<PlayerGraphics>(),
std::make_unique<PlayerInput>()
);
struct state {
Graphics graphics;
/* Physics physics */
};
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
static struct state state {
Graphics("My Game")
};
*appstate = &state;
/* Create the window */
if (!SDL_CreateWindowAndRenderer("Hello World", 800, 600, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
return SDL_APP_CONTINUE;
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_KEY_DOWN ||
event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
auto state = player.processInput(event);
if (GameState::STOPPING == state) {
return SDL_APP_SUCCESS;
}
return SDL_APP_CONTINUE;
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
std::string message("Hello World!");
int w = 0, h = 0;
float x, y;
const float scale = 4.0f;
auto game = static_cast<state*>(appstate);
/* Center the message and scale it up */
SDL_GetRenderOutputSize(renderer, &w, &h);
SDL_SetRenderScale(renderer, scale, scale);
x = ((w / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * message.length()) / 2;
y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
SDL_FRect rect {
.x = 30,
.y = 30,
.w = 100,
.h = 100
};
/* Draw the message */
SDL_SetRenderDrawColor(renderer, 0, 0, 200, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, message.c_str());
SDL_RenderPresent(renderer);
player.update(game->graphics);
game->graphics.show();
return SDL_APP_CONTINUE;
}