Fix: Resize callback won't fire and canvas_get_size would return wrong size.
The canvas_get_size function now returns the number of visible pixels after scaling. The canvas resize callback now fires after a window was created or updated.
This commit is contained in:
parent
3221ab1841
commit
0090f518e9
2 changed files with 33 additions and 10 deletions
|
|
@ -22,6 +22,8 @@ typedef struct CanvasLayer {
|
||||||
|
|
||||||
static int canvas_display = -1;
|
static int canvas_display = -1;
|
||||||
static unsigned int canvas_tex_size = 1024;
|
static unsigned int canvas_tex_size = 1024;
|
||||||
|
static int canvas_width=0;
|
||||||
|
static int canvas_height=0;
|
||||||
static GLFWwindow* canvas_win;
|
static GLFWwindow* canvas_win;
|
||||||
static CanvasLayer *canvas_base;
|
static CanvasLayer *canvas_base;
|
||||||
static CanvasLayer *canvas_overlay;
|
static CanvasLayer *canvas_overlay;
|
||||||
|
|
@ -32,6 +34,14 @@ void glfw_error_callback(int error, const char* description) {
|
||||||
printf("GLFW Error: %d %s", error, description);
|
printf("GLFW Error: %d %s", error, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int min(int a, int b) {
|
||||||
|
return a < b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int max(int a, int b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
// User callbacks
|
// User callbacks
|
||||||
|
|
||||||
void (*canvas_on_close_cb)();
|
void (*canvas_on_close_cb)();
|
||||||
|
|
@ -95,6 +105,9 @@ static void canvas_on_key(GLFWwindow* window, int key, int scancode, int action,
|
||||||
}
|
}
|
||||||
|
|
||||||
static void canvas_on_resize(GLFWwindow* window, int w, int h) {
|
static void canvas_on_resize(GLFWwindow* window, int w, int h) {
|
||||||
|
canvas_width = w;
|
||||||
|
canvas_height = h;
|
||||||
|
|
||||||
if(canvas_on_resize_cb)
|
if(canvas_on_resize_cb)
|
||||||
(*canvas_on_resize_cb)();
|
(*canvas_on_resize_cb)();
|
||||||
}
|
}
|
||||||
|
|
@ -144,6 +157,9 @@ static void canvas_window_setup() {
|
||||||
glfwSetKeyCallback(canvas_win, &canvas_on_key);
|
glfwSetKeyCallback(canvas_win, &canvas_on_key);
|
||||||
glfwSetFramebufferSizeCallback(canvas_win, &canvas_on_resize);
|
glfwSetFramebufferSizeCallback(canvas_win, &canvas_on_resize);
|
||||||
|
|
||||||
|
glfwGetFramebufferSize(canvas_win, &canvas_width, &canvas_height);
|
||||||
|
canvas_on_resize(canvas_win, canvas_width, canvas_height);
|
||||||
|
|
||||||
canvas_do_layout = 0;
|
canvas_do_layout = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -237,19 +253,18 @@ static void* canvas_render_loop(void * arg) {
|
||||||
if (glfwWindowShouldClose(canvas_win))
|
if (glfwWindowShouldClose(canvas_win))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
int w, h;
|
glfwGetFramebufferSize(canvas_win, &canvas_width, &canvas_height);
|
||||||
glfwGetFramebufferSize(canvas_win, &w, &h);
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glOrtho(0, w, h, 0, -1, 1);
|
glOrtho(0, canvas_width, canvas_height, 0, -1, 1);
|
||||||
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
|
glViewport(0, 0, (GLsizei) canvas_width, (GLsizei) canvas_height);
|
||||||
glClearColor(0, 0, 0, 1);
|
glClearColor(0, 0, 0, 1);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
|
|
||||||
GLuint texSize = canvas_base->size;
|
GLuint texSize = canvas_base->size;
|
||||||
if(w > texSize || h > texSize) {
|
if(canvas_width > texSize || canvas_height > texSize) {
|
||||||
float scale = ((float) (w>h?w:h)) / (float) texSize;
|
float scale = ((float) max(canvas_width, canvas_height)) / (float) texSize;
|
||||||
glScalef(scale, scale, 1);
|
glScalef(scale, scale, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -372,8 +387,14 @@ void canvas_get_px(unsigned int x, unsigned int y, uint32_t *rgba) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void canvas_get_size(unsigned int *w, unsigned int *h) {
|
void canvas_get_size(unsigned int *w, unsigned int *h) {
|
||||||
// TODO: Clip on window size
|
int texSize = canvas_base->size;
|
||||||
*w = canvas_base->size;
|
if(canvas_width > texSize || canvas_height > texSize) {
|
||||||
*h = canvas_base->size;
|
float scale = ((float) max(canvas_width, canvas_height)) / texSize;
|
||||||
|
*w = min(texSize, canvas_width/scale);
|
||||||
|
*h = min(texSize, canvas_height/scale);
|
||||||
|
} else {
|
||||||
|
*w = canvas_width;
|
||||||
|
*h = canvas_height;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -129,9 +129,11 @@ void px_on_window_close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
canvas_start(1024, &px_on_window_close);
|
|
||||||
canvas_setcb_key(&px_on_key);
|
canvas_setcb_key(&px_on_key);
|
||||||
canvas_setcb_resize(&px_on_resize);
|
canvas_setcb_resize(&px_on_resize);
|
||||||
|
|
||||||
|
canvas_start(1024, &px_on_window_close);
|
||||||
|
|
||||||
net_start(1337, &px_on_connect, &px_on_read, &px_on_close);
|
net_start(1337, &px_on_connect, &px_on_read, &px_on_close);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue