summaryrefslogtreecommitdiff
path: root/src/window.cpp
diff options
context:
space:
mode:
authorJun Zhang <jun@junz.org>2022-01-30 12:30:02 +0800
committerGitHub <noreply@github.com>2022-01-29 21:30:02 -0700
commitcb7af3f4cac90f95926477b4001f9f80037568d5 (patch)
tree022f4700c796a9935acd3ee0d0fd80a812a78464 /src/window.cpp
parent99b4763f1028e72bb06d8db6c7e8ace469f8989c (diff)
refactor: adjust the project infra. (#1)
* refactor: adjust the project infra. This patch adds cmake build system to the project, and adjust infrastructure stuff. Signed-off-by: Jun Zhang <jun@junz.org> * fix: remove compiler flags that only exist in GCC. Signed-off-by: Jun Zhang <jun@junz.org>
Diffstat (limited to 'src/window.cpp')
-rw-r--r--src/window.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/window.cpp b/src/window.cpp
new file mode 100644
index 0000000..a907aba
--- /dev/null
+++ b/src/window.cpp
@@ -0,0 +1,61 @@
+#include "window.h"
+
+#include <SDL2/SDL.h>
+#include <cstdio>
+
+#include "bitmap.h"
+
+Window::Window(const char* title, int width, int height, int scale)
+ : m_width(width), m_height(height), m_scale(scale)
+{
+ if (SDL_Init(SDL_INIT_VIDEO) != 0) {
+ fprintf(stderr, "Failed to initalize SDL: %s\n", SDL_GetError());
+ exit(1);
+ }
+
+ m_window = SDL_CreateWindow("Nonortho", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width * scale, height * scale, SDL_WINDOW_SHOWN);
+ m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED);
+ m_texture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, width, height);
+
+ m_isRunning = true;
+}
+
+Window::~Window()
+{
+ SDL_DestroyTexture(m_texture);
+ SDL_DestroyRenderer(m_renderer);
+ SDL_DestroyWindow(m_window);
+ SDL_Quit();
+}
+
+void Window::update()
+{
+ static SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ m_isRunning = false;
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ m_mouseDown(event.button.button, event.button.x, event.button.y);
+ break;
+ case SDL_MOUSEMOTION:
+ m_mouseMove(event.button.x, event.button.y);
+ break;
+ case SDL_MOUSEBUTTONUP:
+ m_mouseUp(event.button.button, event.button.x, event.button.y);
+ break;
+ }
+ }
+
+ SDL_UpdateWindowSurface(m_window);
+ SDL_Delay(10);
+}
+
+void Window::draw(Bitmap& bitmap)
+{
+ SDL_RenderClear(m_renderer);
+ SDL_UpdateTexture(m_texture, NULL, bitmap.data, m_width * 4);
+ SDL_RenderCopy(m_renderer, m_texture, NULL, NULL);
+ SDL_RenderPresent(m_renderer);
+} \ No newline at end of file