mirror of
https://github.com/IllusionMan1212/cudoku.git
synced 2026-10-24 02:12:25 +00:00
square board and properly fit inside window
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
compile_commands.json
|
||||
*.o
|
||||
cudoku
|
||||
.cache
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
#include "shader.h"
|
||||
|
||||
static const float quad_vertices[] = {
|
||||
-0.5f, 1.0f, 0.0f, // top left
|
||||
0.5f, 1.0f, 0.0f, // top right
|
||||
-0.5f, -1.0f, 0.0f, // bottom left
|
||||
0.5f, -1.0f, 0.0f, // bottom right
|
||||
-1.0f, 1.0f, 0.0f, // top left
|
||||
1.0f, 1.0f, 0.0f, // top right
|
||||
-1.0f, -1.0f, 0.0f, // bottom left
|
||||
1.0f, -1.0f, 0.0f, // bottom right
|
||||
};
|
||||
|
||||
static const int quad_indices[] = {
|
||||
@@ -34,9 +34,18 @@ unsigned int prepare_bg_rect() {
|
||||
return vao;
|
||||
}
|
||||
|
||||
void draw_bg_rect(Shader shader, unsigned int vao) {
|
||||
void draw_bg_rect(Shader shader, unsigned int vao, float x_scale, float y_scale) {
|
||||
use_shader(shader);
|
||||
|
||||
float mat[4][4] = {
|
||||
{x_scale, 0, 0, 0},
|
||||
{0, y_scale, 0, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1},
|
||||
};
|
||||
|
||||
set_mat4(shader, "transform", (float *)mat);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
#include "shader.h"
|
||||
|
||||
unsigned int prepare_bg_rect(Shader shader);
|
||||
void draw_bg_rect(Shader shader, unsigned int vao);
|
||||
void draw_bg_rect(Shader shader, unsigned int vao, float x_scale, float y_scale);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <glad/glx.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "x11.h"
|
||||
#include "cudoku.h"
|
||||
@@ -12,6 +13,18 @@
|
||||
// Simply port the js algo for sudoku generation
|
||||
// Maybe after the game works fine we can use some winapi to add windows support
|
||||
|
||||
void scale_bg_rect(int width, int height, float *x, float *y) {
|
||||
if (width > height) {
|
||||
float ratio = (float)height / (float)width;
|
||||
*x = ratio;
|
||||
*y = 1.f;
|
||||
} else {
|
||||
float ratio = (float)width / (float)height;
|
||||
*x = 1.f;
|
||||
*y = ratio;
|
||||
}
|
||||
}
|
||||
|
||||
Display *display;
|
||||
Window window;
|
||||
|
||||
@@ -19,7 +32,11 @@ int main() {
|
||||
int res = init_x11(&display, &window);
|
||||
if (res < 0) return 1;
|
||||
|
||||
// 900 is the initial width and height
|
||||
int width = 900, height = 900;
|
||||
// scale factors for the entire board to nicely fit as a square inside
|
||||
// the window
|
||||
float x_scale = 1.f, y_scale = 1.f;
|
||||
|
||||
Shader board_shader = create_shader("shaders/board_v.glsl", "shaders/board_f.glsl");
|
||||
unsigned int board_vao = prepare_bg_rect(board_shader);
|
||||
@@ -37,18 +54,21 @@ int main() {
|
||||
width = xce.width;
|
||||
height = xce.height;
|
||||
resize_x11_window(display, window);
|
||||
scale_bg_rect(xce.width, xce.height, &x_scale, &y_scale);
|
||||
}
|
||||
|
||||
} else if (xev.type == KeyPress && XLookupKeysym(&xev.xkey, 0) == XK_Escape) {
|
||||
quit = true;
|
||||
break;
|
||||
} else if (xev.type == KeyPress) {
|
||||
if (XLookupKeysym(&xev.xkey, 0) == XK_Escape || XLookupKeysym(&xev.xkey, 0) == XK_q) {
|
||||
quit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
draw_bg_rect(board_shader, board_vao);
|
||||
draw_bg_rect(board_shader, board_vao, x_scale, y_scale);
|
||||
|
||||
glXSwapBuffers(display, window);
|
||||
}
|
||||
|
||||
@@ -81,3 +81,8 @@ void use_shader(Shader shader) {
|
||||
glUseProgram(shader.program);
|
||||
}
|
||||
|
||||
void set_mat4(Shader shader, const char *name, float *mat4) {
|
||||
int loc = glGetUniformLocation(shader.program, name);
|
||||
glUniformMatrix4fv(loc, 1, GL_FALSE, mat4);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,3 +7,4 @@ typedef struct Shader {
|
||||
void read_shader_file(const char *path, char **buf);
|
||||
Shader create_shader(const char *vertex_path, const char *fragment_path);
|
||||
void use_shader(Shader shader);
|
||||
void set_mat4(Shader shader, const char *name, float *mat);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 transform;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
gl_Position = transform * vec4(aPos, 1.0f);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user