abstracted window

This commit is contained in:
Samuel Walker 2024-04-23 13:15:47 -06:00
parent 2f8e8a0a91
commit 2c51afc3a3
4 changed files with 60 additions and 16 deletions

View File

@ -4,13 +4,13 @@ CXXFLAGS := -std=c++11 -c
LDFLAGS := -g LDFLAGS := -g
LDLIBS= -lglfw LDLIBS= -lglfw
srcfiles := $(wildcard */*.cpp) srcfiles := $(wildcard src/*.cpp)
objfiles := $(subst src,obj,$(subst .cpp,.o,$(srcfiles))) objfiles := $(subst src,obj,$(subst .cpp,.o,$(srcfiles)))
all: main.out all: main.out
main.out: $(objfiles) main.out: $(objfiles)
$(CXX) $(LDFLAGS) -o $@ $< $(LDLIBS) $(CXX) $(LDFLAGS) -o $@ $(objfiles) $(LDLIBS)
obj/%.o: src/%.cpp obj/%.o: src/%.cpp
mkdir -p obj mkdir -p obj

View File

@ -1,5 +1,7 @@
#include <iostream> #include <iostream>
#include "window.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <ostream>
int main(){ int main(){
std::cout << "Testing from work" << std::endl; std::cout << "Testing from work" << std::endl;
@ -7,21 +9,14 @@ int main(){
std::cerr << "Unable to initialize GLFW. Terminating." << std::endl; std::cerr << "Unable to initialize GLFW. Terminating." << std::endl;
return 0; return 0;
} }
Window* window = new Window(800, 600, "Test Window");
GLFWwindow* window = glfwCreateWindow(800, 600, "Test Window", NULL, NULL); while(!window->closing()){
if(!window){ if(window->mouseDown(0)){
std::cerr << "Window creation failed. Terminating." << std::endl; std::cout << "mouse button down" << std::endl;
glfwTerminate();
return 0;
} }
glfwMakeContextCurrent(window); window->updateWindow();
while(!glfwWindowShouldClose(window)){
// Game Loop
glfwSwapBuffers(window);
glfwPollEvents();
} }
delete window;
glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
return 0; return 0;

31
src/window.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "window.h"
#include <GLFW/glfw3.h>
Window::Window(int width, int height, std::string title){
this->width = width;
this->height = height;
this->title = title;
this->window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
makeContextCurrent();
}
Window::~Window(){
glfwDestroyWindow(window);
}
void Window::makeContextCurrent(){
glfwMakeContextCurrent(window);
}
void Window::updateWindow(){
glfwSwapBuffers(window);
glfwPollEvents();
}
bool Window::closing(){
return glfwWindowShouldClose(window);
}
bool Window::mouseDown(int button){
return glfwGetMouseButton(window, button);
}

18
src/window.h Normal file
View File

@ -0,0 +1,18 @@
#include <GLFW/glfw3.h>
#include <string>
class Window{
private:
GLFWwindow* window;
std::string title;
int width, height;
public:
Window(int width, int height, std::string title);
~Window();
void updateWindow();
void makeContextCurrent();
bool closing();
bool mouseDown(int button);
};