Files
Frostbite2D/Fostbite2D/src/fostbite2D/core/window.cpp
Lenheart ce2cc4d210 refactor(core): 重构代码组织结构,将类型定义移至types目录
将核心类型定义从core目录迁移至新创建的types目录,包括:
- 基础类型别名(type_alias.h)
- 数学相关类型(type_math.h)
- 颜色类型(type_color.h)
- 窗口配置合并至application.h
删除不再使用的平台配置和旧类型文件
优化头文件包含关系,减少编译依赖
2026-02-21 02:46:19 +08:00

150 lines
3.6 KiB
C++

#include "SDL_log.h"
#include <SDL2/SDL.h>
#include <fostbite2D/core/window.h>
#include <glad/glad.h>
namespace frostbite2D {
bool Window::create(const WindowConfig &cfg) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
return false;
}
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
#ifdef __SWITCH__
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
#else
if (cfg.fullscreen) {
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
} else if (cfg.borderless) {
flags |= SDL_WINDOW_BORDERLESS;
}
if (cfg.resizable) {
flags |= SDL_WINDOW_RESIZABLE;
}
if (!cfg.decorated) {
flags |= SDL_WINDOW_BORDERLESS;
}
#endif
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
if (cfg.multisamples > 0) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, cfg.multisamples);
}
int x = SDL_WINDOWPOS_CENTERED;
int y = SDL_WINDOWPOS_CENTERED;
if (!cfg.centered) {
x = SDL_WINDOWPOS_UNDEFINED;
y = SDL_WINDOWPOS_UNDEFINED;
}
// 创建窗口
sdlWindow_ =
SDL_CreateWindow(cfg.title.c_str(), x, y, cfg.width, cfg.height, flags);
if (!sdlWindow_) {
SDL_Log("Failed to create window: %s", SDL_GetError());
SDL_Quit();
return false;
}
glContext_ = SDL_GL_CreateContext(sdlWindow_);
if (!glContext_) {
SDL_Log("Failed to create OpenGL context: %s", SDL_GetError());
SDL_DestroyWindow(sdlWindow_);
sdlWindow_ = nullptr;
SDL_Quit();
return false;
}
if (!gladLoadGLES2Loader((GLADloadproc)SDL_GL_GetProcAddress)) {
SDL_Log("Failed to initialize GLAD GLES2");
SDL_GL_DeleteContext(glContext_);
glContext_ = nullptr;
SDL_DestroyWindow(sdlWindow_);
sdlWindow_ = nullptr;
SDL_Quit();
return false;
}
SDL_GL_SetSwapInterval(cfg.vsync ? 1 : 0);
SDL_GetWindowSize(sdlWindow_, &width_, &height_);
fullscreen_ = (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
vsync_ = cfg.vsync;
return true;
}
void Window::destroy() {}
void Window::poll() {}
void Window::swap() {}
void Window::close() { shouldClose_ = true; }
void Window::setTitle(const std::string &title) {}
void Window::setSize(int w, int h) {
width_ = w;
height_ = h;
}
void Window::setPos(int x, int y) {}
void Window::setFullscreen(bool fs) { fullscreen_ = fs; }
void Window::setVSync(bool vsync) { vsync_ = vsync; }
void Window::setVisible(bool visible) {}
int Window::width() const { return width_; }
int Window::height() const { return height_; }
Size Window::size() const {
return Size{static_cast<float>(width_), static_cast<float>(height_)};
}
Vec2 Window::pos() const { return Vec2{0.0f, 0.0f}; }
bool Window::fullscreen() const { return fullscreen_; }
bool Window::vsync() const { return vsync_; }
bool Window::focused() const { return focused_; }
bool Window::minimized() const { return minimized_; }
float Window::scaleX() const { return scaleX_; }
float Window::scaleY() const { return scaleY_; }
void Window::setCursor(CursorType cursor) {}
void Window::showCursor(bool show) { cursorVisible_ = show; }
void Window::lockCursor(bool lock) { cursorLocked_ = lock; }
void Window::onResize(ResizeCb cb) { resizeCb_ = cb; }
void Window::onClose(CloseCb cb) { closeCb_ = cb; }
void Window::onFocus(FocusCb cb) { focusCb_ = cb; }
void *Window::native() const { return nullptr; }
} // namespace frostbite2D