mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
A startup for it
This commit is contained in:
99
src/main.cpp
99
src/main.cpp
@@ -1,67 +1,72 @@
|
||||
/*
|
||||
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely.
|
||||
*/
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 // 启用回调函数模式
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include "core/Game.h"
|
||||
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
/* 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());
|
||||
//使用SDL3的appstate进行隔离,避免全局变量
|
||||
struct AppState {
|
||||
std::unique_ptr<Game> game;
|
||||
};
|
||||
|
||||
|
||||
|
||||
static const int WIDTH = 1600;
|
||||
static const int HEIGHT = 900;
|
||||
|
||||
|
||||
// 1. 应用程序初始化回调
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
|
||||
// 设置应用元数据[8](@ref)
|
||||
SDL_SetAppMetadata("孢子棋", "1.0.0", "com.zhenyan121.sporechess");
|
||||
|
||||
// 初始化SDL视频子系统[6,7](@ref)
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_Log("SDL初始化失败: %s", SDL_GetError());
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
// 创建游戏实例
|
||||
auto state = new AppState();
|
||||
state->game = std::make_unique<Game>(WIDTH, HEIGHT);
|
||||
|
||||
|
||||
if (!state->game->initialize()) {
|
||||
SDL_Log("游戏初始化失败!");
|
||||
delete state;
|
||||
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)
|
||||
{
|
||||
// 2. 事件处理回调
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
||||
auto s = static_cast<AppState*>(appstate);
|
||||
if (event->type == SDL_EVENT_KEY_DOWN ||
|
||||
event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
const char *message = "Hello World!";
|
||||
int w = 0, h = 0;
|
||||
float x, y;
|
||||
const float scale = 4.0f;
|
||||
|
||||
/* 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 * SDL_strlen(message)) / 2;
|
||||
y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
|
||||
|
||||
/* Draw the message */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
SDL_RenderDebugText(renderer, x, y, message);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// 3. 主循环迭代回调(每帧调用)
|
||||
SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||
auto s = static_cast<AppState*>(appstate);
|
||||
if (s->game) { //防止空指针
|
||||
// 更新游戏逻辑
|
||||
|
||||
|
||||
// 渲染帧
|
||||
s->game->render();
|
||||
}
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result)
|
||||
{
|
||||
// 4. 应用退出回调(清理资源)
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||
delete static_cast<AppState*>(appstate);
|
||||
SDL_Quit();
|
||||
}
|
||||
Reference in New Issue
Block a user