Begin Repo

This commit is contained in:
2024-08-10 21:20:28 -05:00
commit 08115f90ce
41 changed files with 19581 additions and 0 deletions

87
window/glfwWindow.cpp Normal file
View File

@@ -0,0 +1,87 @@
#include "glfwWindow.h"
WindowGLFW::WindowGLFW() {
}
WindowGLFW::~WindowGLFW() {
glfwTerminate();
}
void WindowGLFW::init( std::string title, int x, int y) : title(title), x(x), y(y) {
if(!glfwInit()) {
std::cout << "glfwInit failed!\n";
error = true;
return;
}
window = glfwCreateWindow(x, y, title.c_str(), NULL, NULL);
if(!window) {
glfwTerminate();
std::cout << "glfwCreateWindow failed!";
error = true;
return;
}
glfwMakeContextCurrent(window);
}
void WindowGLFW::shutdown() {
~WindowGLFW();
}
void WindowGLFW::getSize() {
SDL_GetWindowSize(window, &x, &y);
}
const Event* WindowGLFW::createEvent() {
switch(event.type) {
case SDL_KEYDOWN:
return new KeyEvent(event.key.keysym.sym, event.key.keysym.scancode,
event.key.keysym.mod, event.key.repeat, true);
case SDL_KEYUP:
return new KeyEvent(event.key.keysym.sym, event.key.keysym.scancode,
event.key.keysym.mod, event.key.repeat, false);
case SDL_TEXTINPUT:
return new KeyTypedEvent(event.text.windowID, event.text.text);
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
switch(event.button.button) {
case SDL_BUTTON_LEFT: return new MouseButtonEvent(0, (event.type == SDL_MOUSEBUTTONDOWN));
case SDL_BUTTON_RIGHT: return new MouseButtonEvent(1, (event.type == SDL_MOUSEBUTTONDOWN));
case SDL_BUTTON_MIDDLE: return new MouseButtonEvent(2, (event.type == SDL_MOUSEBUTTONDOWN));
case SDL_BUTTON_X1: return new MouseButtonEvent(3, (event.type == SDL_MOUSEBUTTONDOWN));
case SDL_BUTTON_X2: return new MouseButtonEvent(4, (event.type == SDL_MOUSEBUTTONDOWN));
}
case SDL_MOUSEMOTION:
return new MouseMovedEvent(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
case SDL_MOUSEWHEEL:
return new MouseScrolledEvent(event.wheel.x, event.wheel.y);
case SDL_WINDOWEVENT:
return new WindowEvent(event.window.windowID, event.window.event);
}
return nullptr;
}
const Event* WindowGLFW::pollEvents() {
while (SDL_PollEvent(&event)) {
return createEvent();
}
return nullptr;
}

42
window/glfwWindow.h Normal file
View File

@@ -0,0 +1,42 @@
#pragma once
#include "terminal.h"
#include "event.h"
#include "mouseEvent.h"
#include "keyEvent.h"
#include "windowEvent.h"
class WindowGLFW {
public:
WindowGLFW();
~WindowGLFW();
void init( std::string, int, int);
void shutdown();
void getSize();
const Event* pollEvents();
operator GLFWwindow*() { return window; }
//private:
const Event* createEvent();
int x, y;
std::string title;
GLFWwindow* window = nullptr;
SDL_Event event;
bool error = false;
};

94
window/sdl2Window.cpp Normal file
View File

@@ -0,0 +1,94 @@
#include "sdl2Window.h"
WindowSDL2::WindowSDL2() {
}
WindowSDL2::~WindowSDL2() {
SDL_DestroyWindow(window);
SDL_Quit();
}
void WindowSDL2::init( std::string title, int x, int y) : title(title), x(x), y(y) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
error = true;
return;
}
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, x, y, window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
error = true;
return;
}
// int test = SDL_SetWindowOpacity(this->window, 0.1f);
// float t;
// SDL_GetWindowOpacity(this->window, &t);
// std::cout << test << " " << t << std::endl;
}
void WindowSDL2::shutdown() {
~WindowSDL2();
}
void WindowSDL2::getSize() {
SDL_GetWindowSize(window, &x, &y);
}
const Event* WindowSDL2::createEvent() {
switch(event.type) {
case SDL_KEYDOWN:
return new KeyEvent(event.key.keysym.sym, event.key.keysym.scancode,
event.key.keysym.mod, event.key.repeat, true);
case SDL_KEYUP:
return new KeyEvent(event.key.keysym.sym, event.key.keysym.scancode,
event.key.keysym.mod, event.key.repeat, false);
case SDL_TEXTINPUT:
return new KeyTypedEvent(event.text.windowID, event.text.text);
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
switch(event.button.button) {
case SDL_BUTTON_LEFT: return new MouseButtonEvent(0, (event.type == SDL_MOUSEBUTTONDOWN));
case SDL_BUTTON_RIGHT: return new MouseButtonEvent(1, (event.type == SDL_MOUSEBUTTONDOWN));
case SDL_BUTTON_MIDDLE: return new MouseButtonEvent(2, (event.type == SDL_MOUSEBUTTONDOWN));
case SDL_BUTTON_X1: return new MouseButtonEvent(3, (event.type == SDL_MOUSEBUTTONDOWN));
case SDL_BUTTON_X2: return new MouseButtonEvent(4, (event.type == SDL_MOUSEBUTTONDOWN));
}
case SDL_MOUSEMOTION:
return new MouseMovedEvent(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
case SDL_MOUSEWHEEL:
return new MouseScrolledEvent(event.wheel.x, event.wheel.y);
case SDL_WINDOWEVENT:
return new WindowEvent(event.window.windowID, event.window.event);
}
return nullptr;
}
const Event* WindowSDL2::pollEvents() {
while (SDL_PollEvent(&event)) {
return createEvent();
}
return nullptr;
}

42
window/sdl2Window.h Normal file
View File

@@ -0,0 +1,42 @@
#pragma once
#include "terminal.h"
#include "event.h"
#include "mouseEvent.h"
#include "keyEvent.h"
#include "windowEvent.h"
class WindowSDL2 {
public:
WindowSDL2();
~WindowSDL2();
void init( std::string, int, int);
void shutdown();
void getSize();
const Event* pollEvents();
operator SDL_Window*() { return window; }
//private:
const Event* createEvent();
int x, y;
std::string title;
SDL_Window* window = nullptr;
SDL_Event event;
bool error = false;
};

31
window/window.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include "window.h"
Window::Window()
{
}
Window::~Window()
{
}
void Window::init(std::string, int, int)
{
}
void Window::shutdown()
{
}
void Window::getSize()
{
}
const Event *Window::pollEvents()
{
return nullptr;
}
const Event *Window::createEvent()
{
return nullptr;
}

42
window/window.h Normal file
View File

@@ -0,0 +1,42 @@
#pragma once
#include "terminal.h"
#include "event.h"
#include "mouseEvent.h"
#include "keyEvent.h"
#include "windowEvent.h"
class Window {
public:
Window();
~Window();
void init( std::string, int, int);
void shutdown();
void getSize();
const Event* pollEvents();
operator SDL_Window*() { return window; }
//private:
const Event* createEvent();
int x, y;
std::string title;
SDL_Window* window = nullptr;
SDL_Event event;
bool error = false;
};