Skip to content

Commit

Permalink
DesktopDisplay and Display (for android)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Jan 12, 2024
1 parent 8dd8f15 commit bb8d078
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 219 deletions.
4 changes: 2 additions & 2 deletions examples/Particles/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main(int argv, char ** argc)
}
}

jGL::Display display(resX, resY, "Particles", vulkan);
jGL::DesktopDisplay display(glm::ivec2(resX, resY), "Particles", vulkan);

glewInit();

Expand All @@ -22,7 +22,7 @@ int main(int argv, char ** argc)
}
else
{
jGLInstance = std::move(std::make_unique<jGL::GL::OpenGLInstance>(display));
jGLInstance = std::move(std::make_unique<jGL::GL::OpenGLInstance>(display.getRes()));
}

jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0));
Expand Down
2 changes: 1 addition & 1 deletion examples/Particles/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <jGL/Vulkan/vulkanInstance.h>

#include <logo.h>
#include <jGL/Display/display.h>
#include <jGL/Display/desktopDisplay.h>
#include <jGL/orthoCam.h>

#include <dynamics.h>
Expand Down
4 changes: 2 additions & 2 deletions examples/Shape/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
int main(int argv, char ** argc)
{

jGL::Display display(resX, resY, "Shape");
jGL::DesktopDisplay display(glm::ivec2(resX, resY), "Shape");

glewInit();

jGLInstance = std::move(std::make_unique<jGL::GL::OpenGLInstance>(display));
jGLInstance = std::move(std::make_unique<jGL::GL::OpenGLInstance>(display.getRes()));

jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0));

Expand Down
8 changes: 4 additions & 4 deletions examples/Shape/main.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef MAIN
#define MAIN
#ifndef MAIN_H
#define MAIN_H

#include <jGL/jGL.h>
#include <jGL/OpenGL/openGLInstance.h>
#include <jGL/OpenGL/Shader/glShader.h>
#include <jGL/shape.h>

#include <logo.h>
#include <jGL/Display/display.h>
#include <jGL/Display/desktopDisplay.h>
#include <jGL/orthoCam.h>

#include <jLog/jLog.h>
Expand Down Expand Up @@ -58,4 +58,4 @@ const char * fragmentShader =
"fragment = colour;\n"
"}";

#endif /* MAIN */
#endif /* MAIN_H */
4 changes: 2 additions & 2 deletions examples/Sprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
int main(int argv, char ** argc)
{

jGL::Display display(resX, resY, "Sprite");
jGL::DesktopDisplay display(glm::ivec2(resX, resY), "Sprite");

glewInit();

jGLInstance = std::move(std::make_unique<jGL::GL::OpenGLInstance>(display));
jGLInstance = std::move(std::make_unique<jGL::GL::OpenGLInstance>(display.getRes()));

jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0));

Expand Down
2 changes: 1 addition & 1 deletion examples/Sprite/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <jGL/OpenGL/openGLInstance.h>

#include <logo.h>
#include <jGL/Display/display.h>
#include <jGL/Display/desktopDisplay.h>
#include <jGL/orthoCam.h>

#include <jLog/jLog.h>
Expand Down
176 changes: 176 additions & 0 deletions include/jGL/Display/desktopDisplay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#ifndef DesktopDisplay_H
#define DesktopDisplay_H

#include <cstdlib>
#include <vector>
#include <map>

#include <vulkan/vulkan.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <jGL/Display/event.h>
#include <jGL/Display/display.h>

namespace jGL
{

void defaultKeyEventCallback
(
GLFWwindow * window,
int key,
int scancode,
int action,
int mods
);

class DesktopDisplay : public Display
{
public:

DesktopDisplay
(
glm::ivec2 res,
const char * title,
GLFWkeyfun keyCallback,
GLFWmousebuttonfun mouseButtonCallback,
GLFWscrollfun mouseScrollCallback,
bool vulkan = false
);

DesktopDisplay
(
glm::ivec2 res,
const char * title,
bool vulkan = false
);

~DesktopDisplay(){ glfwTerminate(); free(logo); }

GLFWwindow * getWindow() const { return glfwWindow; }

bool isOpen(){ if (glfwWindow != NULL) { return !glfwWindow ? false : true; } return false; }
bool closing(){ return glfwWindowShouldClose(glfwWindow); }

void open(){
if (glfwWindow == NULL)
{
// required for MacOS
// https://www.glfw.org/faq.html#41__how_do_i_create_an_opengl_30_context
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindow = glfwCreateWindow(getResX(), getResY(),title,NULL,NULL); glfwSwapInterval(1);
}
}

void close(){ if (glfwWindow != NULL) { glfwDestroyWindow(glfwWindow); glfwWindow = NULL; } }

void setAsFocus(){ if (glfwWindow != NULL) { glfwMakeContextCurrent(glfwWindow); } }

void keyCallback
(
GLFWwindow * window,
int key,
int scancode,
int action,
int mods
);

void mousePosition(double & x, double & y){ if (glfwWindow != NULL){ glfwGetCursorPos(glfwWindow,&x,&y); } }

void setMousePosition(double x, double y)
{
if (glfwWindow != NULL)
{
glfwSetCursorPos(glfwWindow, x, y);
}
}

int getKeyLastState(int key) { return glfwGetKey(glfwWindow, key); }

void loop()
{
data.clear();
swap();
handleEvents();
if (glfwWindowShouldClose(glfwWindow)){ close(); }
}

std::vector<Event> getEvents(int code)
{
if (data.events.find(code) == data.events.cend())
{
return {Event()};
}
else
{
return data.events[code];
}
}

std::vector<EventType> getEventTypes(int code)
{
std::vector<EventType> e;
if (data.events.find(code) == data.events.cend())
{
return {EventType::NONE};
}
else
{
for (auto evt : data.events[code])
{
e.push_back(evt.type);
}
return e;
}
}

Event getEvent(int code)
{
if (data.events.find(code) == data.events.cend())
{
return Event();
}
else
{
return data.events[code][0];
}
}

struct WindowData
{
std::map<int, std::vector<Event>> events;
double scrollX = 0.0;
double scrollY = 0.0;
bool scrolled = false;

void clear()
{
scrollX = 0.0;
scrollY = 0.0;
scrolled = false;
events.clear();
}
};

private:

const char * title;

GLFWimage * logo;

GLFWwindow * glfwWindow;

WindowData data;

void swap(){ if (glfwWindow != NULL) { glfwSwapBuffers(glfwWindow); } }

void handleEvents(){ if (glfwWindow != NULL){ glfwPollEvents(); } }

};
}

#endif /* DesktopDisplay_H */
Loading

0 comments on commit bb8d078

Please sign in to comment.