mirror of
https://github.com/IllusionMan1212/cudoku.git
synced 2026-10-24 02:12:25 +00:00
text drawing using freetype
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
BIN=cudoku
|
||||
CC=gcc
|
||||
CFLAGS=-Wall -Wextra -Werror -pedantic -O3 -g `pkg-config --cflags x11` -I3rdparty/glad/include -I3rdparty/stb
|
||||
OBJ=main.o x11.o cudoku.o shader.o 3rdparty/glad/src/gl.o 3rdparty/glad/src/glx.o 3rdparty/stb/stb.o
|
||||
LDFLAGS=`pkg-config --libs x11` -lm
|
||||
CFLAGS=-Wall -Wextra -pedantic -O3 -g `pkg-config --cflags freetype2` -I3rdparty/glad/include -I3rdparty/stb
|
||||
OBJ=main.o x11.o cudoku.o shader.o font.o 3rdparty/glad/src/gl.o 3rdparty/glad/src/glx.o 3rdparty/stb/stb.o
|
||||
LDFLAGS=`pkg-config --libs x11 freetype2` -lm
|
||||
DEPS=3rdparty/glad/include/glad/gl.h 3rdparty/glad/include/glad/glx.h 3rdparty/stb/stb_image.h
|
||||
|
||||
%.o: %.c $(DEPS)
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
Binary file not shown.
@@ -0,0 +1,93 @@
|
||||
Copyright 2015 The Rubik Project Authors (https://github.com/googlefonts/rubik)
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
http://scripts.sil.org/OFL
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font creation
|
||||
efforts of academic and linguistic communities, and to provide a free and
|
||||
open framework in which fonts may be shared and improved in partnership
|
||||
with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply
|
||||
to any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components as
|
||||
distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting -- in part or in whole -- any of the components of the
|
||||
Original Version, by changing formats or by porting the Font Software to a
|
||||
new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,
|
||||
in Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the corresponding
|
||||
Copyright Holder. This restriction only applies to the primary font name as
|
||||
presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created
|
||||
using the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
||||
Binary file not shown.
@@ -1,9 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include "3rdparty/stb/stb_image.h"
|
||||
|
||||
#include "cudoku.h"
|
||||
#include "font.h"
|
||||
#include "shader.h"
|
||||
|
||||
static const float quad_vertices[] = {
|
||||
@@ -31,7 +34,7 @@ void set_scale_factor(int width, int height, float *x, float *y) {
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int prepare_bg(bool use_texture) {
|
||||
unsigned int prepare_bg(bool use_texture, unsigned int *texture) {
|
||||
unsigned int vbo, vao, ebo;
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo);
|
||||
@@ -49,12 +52,11 @@ unsigned int prepare_bg(bool use_texture) {
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
||||
|
||||
if (use_texture) {
|
||||
unsigned int texture;
|
||||
int tex_width = 0, tex_height = 0;
|
||||
unsigned char *tex_data = NULL;
|
||||
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glGenTextures(1, &(*texture));
|
||||
glBindTexture(GL_TEXTURE_2D, *texture);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
@@ -80,20 +82,38 @@ unsigned int prepare_bg(bool use_texture) {
|
||||
return vao;
|
||||
}
|
||||
|
||||
void draw_bg_grid_shader(Shader shader, int vao, float* transform, float resolution) {
|
||||
void draw_bg_grid_shader(Shader shader, unsigned int vao, float* transform, float resolution) {
|
||||
use_shader(shader);
|
||||
|
||||
set_mat4(shader, "transform", transform);
|
||||
set_mat4f(shader, "transform", transform);
|
||||
set_float(shader, "resolution", resolution);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
void draw_bg_grid_texture(Shader shader, int vao, float* transform) {
|
||||
void draw_bg_grid_texture(Shader shader, unsigned int vao, unsigned int texture, float* transform) {
|
||||
use_shader(shader);
|
||||
set_mat4(shader, "transform", transform);
|
||||
set_mat4f(shader, "transform", transform);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
void draw_numbers(Shader shader, unsigned int vao, unsigned int vbo, float *transform, int board[9][9]) {
|
||||
// TODO: go thru the board state and call draw_text() with the appropriate number
|
||||
// and position
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
draw_number(shader, board[i][j], i, j, 5.0f, vao, vbo, transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_win_overlay(Shader shader, unsigned int vao, unsigned int vbo, float *transform) {
|
||||
const char *text = "You won!";
|
||||
Size text_size = calculate_text_size(text, 1.0f);
|
||||
draw_text(shader, text, text_size, 1.0f, vao, vbo, transform);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "shader.h"
|
||||
|
||||
void set_scale_factor(int width, int height, float *x, float *y);
|
||||
unsigned int prepare_bg(bool use_texture);
|
||||
void draw_bg_grid_shader(Shader shader, int vao, float* transform, float resolution);
|
||||
void draw_bg_grid_texture(Shader shader, int vao, float* transform);
|
||||
unsigned int prepare_bg(bool use_texture, unsigned int *texture);
|
||||
void draw_bg_grid_shader(Shader shader, unsigned int vao, float* transform, float resolution);
|
||||
void draw_bg_grid_texture(Shader shader, unsigned int vao, unsigned int texture, float* transform);
|
||||
void draw_numbers(Shader shader, unsigned int vao, unsigned int vbo, float *transform, int board[9][9]);
|
||||
void draw_win_overlay(Shader shader, unsigned int vao, unsigned int vbo, float *transform);
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include <freetype/ftmm.h>
|
||||
#include <glad/gl.h>
|
||||
|
||||
#include "font.h"
|
||||
#include "shader.h"
|
||||
|
||||
Character numbers[9];
|
||||
Character characters[128];
|
||||
|
||||
int init_fonts(const char *font_path) {
|
||||
FT_Library ft;
|
||||
if (FT_Init_FreeType(&ft)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
FT_Face face;
|
||||
if (FT_New_Face(ft, font_path, 0, &face)) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if ((face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) {
|
||||
printf("[INFO] Got a variable font\n");
|
||||
FT_MM_Var *mm;
|
||||
FT_Get_MM_Var(face, &mm);
|
||||
|
||||
FT_Set_Var_Design_Coordinates(face, mm->num_namedstyles, mm->namedstyle[mm->num_namedstyles - 4].coords);
|
||||
|
||||
FT_Done_MM_Var(ft, mm);
|
||||
}
|
||||
|
||||
FT_Set_Pixel_Sizes(face, 0, 128);
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (FT_Load_Char(face, '0' + (i + 1), FT_LOAD_RENDER)) {
|
||||
printf("Error: failed to load glyph for number '%c'\n", '0' + i);
|
||||
}
|
||||
|
||||
unsigned int texture;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width,
|
||||
face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
|
||||
face->glyph->bitmap.buffer);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
numbers[i].texture_id = texture;
|
||||
numbers[i].advance = face->glyph->advance.x;
|
||||
Size s = { .width = face->glyph->bitmap.width, .height = face->glyph->bitmap.rows };
|
||||
numbers[i].size = s;
|
||||
Size bearing = { .width = face->glyph->bitmap_left, .height = face->glyph->bitmap_top };
|
||||
numbers[i].bearing = bearing;
|
||||
}
|
||||
|
||||
for (int c = 0; c < 128; c++) {
|
||||
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
|
||||
printf("Error: failed to load glyph for char '%c'\n", c);
|
||||
}
|
||||
|
||||
unsigned int texture;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width,
|
||||
face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
|
||||
face->glyph->bitmap.buffer);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
characters[c].texture_id = texture;
|
||||
characters[c].advance = face->glyph->advance.x;
|
||||
Size s = { .width = face->glyph->bitmap.width, .height = face->glyph->bitmap.rows };
|
||||
characters[c].size = s;
|
||||
Size bearing = { .width = face->glyph->bitmap_left, .height = face->glyph->bitmap_top };
|
||||
characters[c].bearing = bearing;
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
FT_Done_Face(face);
|
||||
FT_Done_FreeType(ft);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void prepare_font(unsigned int *vao, unsigned int *vbo) {
|
||||
glGenVertexArrays(1, &(*vao));
|
||||
glGenBuffers(1, &(*vbo));
|
||||
|
||||
glBindVertexArray(*vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, *vbo);
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6*4, NULL, GL_DYNAMIC_DRAW);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
Size calculate_text_size(const char *text, float scale) {
|
||||
Size size = { .width = 0, .height = 0 };
|
||||
float w = 0;
|
||||
float h = 0;
|
||||
for (uint i = 0; i < strlen(text); i++) {
|
||||
Character ch = characters[(uint)text[i]];
|
||||
w += ch.advance / 64.0;
|
||||
if (ch.size.height > h) {
|
||||
h = ch.size.height;
|
||||
}
|
||||
}
|
||||
size.width = w * scale;
|
||||
size.height = h * scale;
|
||||
return size;
|
||||
}
|
||||
|
||||
void draw_number(Shader shader, int text, int row, int column, float scale, unsigned int vao, unsigned int vbo, float *transform) {
|
||||
use_shader(shader);
|
||||
set_vec3f(shader, "textColor", 44.f / 255.f, 54.f / 255.f, 57.f / 255.f);
|
||||
set_mat4f(shader, "transform", transform);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
Character ch = numbers[text - 1];
|
||||
|
||||
float w = ch.size.width;
|
||||
float h = ch.size.height;
|
||||
float ratio = w / h;
|
||||
|
||||
// draw the glyph over the entire board and then scale it down to the size
|
||||
// of a single cell
|
||||
float vert_h = 1.0;
|
||||
float vert_w = vert_h * ratio;
|
||||
// get size of a single cell in opengl coords (-1, 1)
|
||||
float single_cell = (1.f/9.f) * 2;
|
||||
// we minus 4 since we start at column and row 4 (0-indexed)
|
||||
float offset_x = single_cell * (column - 4);
|
||||
float offset_y = single_cell * (row - 4);
|
||||
|
||||
// texture coords are y-flipped
|
||||
float vertices[6][4] = {
|
||||
// top left tri
|
||||
{ ((-(vert_w) / w * ratio) * scale) + offset_x, (-vert_h / h * scale) - offset_y, 0.0f, 1.0f },
|
||||
{ ((-(vert_w) / w * ratio) * scale) + offset_x, ( vert_h / h * scale) - offset_y, 0.0f, 0.0f },
|
||||
{ (( (vert_w) / w * ratio) * scale) + offset_x, ( vert_h / h * scale) - offset_y, 1.0f, 0.0f },
|
||||
|
||||
// bottom right tri
|
||||
{ ((-(vert_w) / w * ratio) * scale) + offset_x, (-vert_h / h * scale) - offset_y, 0.0f, 1.0f },
|
||||
{ (( (vert_w) / w * ratio) * scale) + offset_x, ( vert_h / h * scale) - offset_y, 1.0f, 0.0f },
|
||||
{ (( (vert_w) / w * ratio) * scale) + offset_x, (-vert_h / h * scale) - offset_y, 1.0f, 1.0f }
|
||||
};
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, ch.texture_id);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void draw_text(Shader shader, const char *text, Size text_size, float scale, unsigned int vao, unsigned int vbo, float *transform) {
|
||||
use_shader(shader);
|
||||
set_vec3f(shader, "textColor", 55.f / 255.f, 55.f / 255.f, 55.f / 255.f);
|
||||
set_mat4f(shader, "transform", transform);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
int c = 0;
|
||||
int x = 0;
|
||||
while (text[c] != '\0') {
|
||||
Character ch = characters[(int)text[c]];
|
||||
|
||||
float xpos = ((x + ch.bearing.width * scale) / 900.f) - ((text_size.width / 2.f) * scale / 900.f);
|
||||
float ypos = (((ch.bearing.height - ch.size.height) * scale) / 900.f) - ((text_size.height / 2.f) * scale / 900.f);
|
||||
|
||||
float w = (ch.size.width * scale / 900.f);
|
||||
float h = (ch.size.height * scale / 900.f);
|
||||
|
||||
float vertices[6][4] = {
|
||||
// top left tri
|
||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||
{ xpos, ypos, 0.0f, 1.0f },
|
||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||
|
||||
// bottom right tri
|
||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||
{ xpos + w, ypos + h, 1.0f, 0.0f }
|
||||
};
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, ch.texture_id);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
||||
x += (ch.advance >> 6) * scale;
|
||||
c++;
|
||||
}
|
||||
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "shader.h"
|
||||
|
||||
typedef struct Size {
|
||||
int width;
|
||||
int height;
|
||||
} Size;
|
||||
|
||||
typedef struct Character {
|
||||
unsigned int texture_id;
|
||||
Size size;
|
||||
Size bearing;
|
||||
unsigned int advance;
|
||||
} Character;
|
||||
|
||||
int init_fonts(const char *font_path);
|
||||
void prepare_font(unsigned int *vao, unsigned int *vbo);
|
||||
Size calculate_text_size(const char *text, float scale);
|
||||
void draw_number(Shader shader, int text, int row, int column, float scale,unsigned int vao, unsigned int vbo, float* transform);
|
||||
void draw_text(Shader shader, const char *text, Size text_size, float scale, unsigned int vao, unsigned int vbo, float *projection);
|
||||
@@ -3,57 +3,109 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <glad/glx.h>
|
||||
#include <time.h>
|
||||
#include "3rdparty/stb/stb_image.h"
|
||||
|
||||
#include "x11.h"
|
||||
#include "cudoku.h"
|
||||
|
||||
// TODO: everything from scratch
|
||||
// X11 for windowing and input
|
||||
// OpenGL for graphics
|
||||
// OpenAL for audio ?? (copilot suggested this. I do want to do some audio desu)
|
||||
// Simply port the js algo for sudoku generation
|
||||
// Maybe after the game works fine we can use some winapi to add windows support
|
||||
#include "font.h"
|
||||
|
||||
void usage() {
|
||||
printf("Usage: cudoku [OPTION]\n\n");
|
||||
printf(" %-20s%-20s", "-h, --help", "prints this help message\n");
|
||||
printf(" %-20s%-20s", "--use-texture", "use a texture instead of generating the grid using shaders\n");
|
||||
printf(" %-20s%-20s", "-f, --font FONT", "use a custom font file to render text\n");
|
||||
}
|
||||
|
||||
Display *display;
|
||||
Window window;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
const char *font_path = "assets/fonts/Rubik/Rubik-VariableFont_wght.ttf";
|
||||
/* const char *font_path = "assets/fonts/Roboto/Roboto-Bold.ttf"; */
|
||||
bool use_texture = false;
|
||||
|
||||
if (argc > 1) {
|
||||
char *flag = argv[1];
|
||||
if (strcmp(flag, "--use-texture") == 0) {
|
||||
use_texture = true;
|
||||
}
|
||||
if (strcmp(flag, "-h") == 0 || strcmp(flag, "--help") == 0) {
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
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 grid_shader;
|
||||
if (!use_texture)
|
||||
grid_shader = create_shader("shaders/board_v.glsl", "shaders/grid_f.glsl");
|
||||
else
|
||||
grid_shader = create_shader("shaders/board_v.glsl", "shaders/board_f.glsl");
|
||||
if (argc > 1) {
|
||||
char *flag = argv[1];
|
||||
if (strcmp(flag, "-h") == 0 || strcmp(flag, "--help") == 0) {
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vao = prepare_bg(use_texture);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
char *flag = argv[i];
|
||||
|
||||
if (strcmp(flag, "-f") == 0 || strcmp(flag, "--font") == 0) {
|
||||
if (i + 1 < argc) {
|
||||
font_path = argv[i + 1];
|
||||
i++;
|
||||
} else {
|
||||
printf("Error: used font flag with no provided font, defaulting to Rubik\n");
|
||||
}
|
||||
} else if (strcmp(flag, "--use-texture") == 0) {
|
||||
use_texture = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int res = init_x11(&display, &window);
|
||||
if (res < 0) return 1;
|
||||
|
||||
res = init_fonts(font_path);
|
||||
if (res == -1) {
|
||||
printf("Error: could not initialize freetype library\n");
|
||||
return 1;
|
||||
} else if (res == -2) {
|
||||
printf("Error: could not load font file: \"%s\"\n", font_path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned int font_vao, font_vbo;
|
||||
prepare_font(&font_vao, &font_vbo);
|
||||
Shader font_shader = create_shader("shaders/font_v.vert", "shaders/font_f.frag");
|
||||
|
||||
Shader grid_shader;
|
||||
unsigned int grid_texture;
|
||||
if (!use_texture)
|
||||
grid_shader = create_shader("shaders/board_v.vert", "shaders/grid_f.frag");
|
||||
else
|
||||
grid_shader = create_shader("shaders/board_v.vert", "shaders/board_f.frag");
|
||||
|
||||
int vao = prepare_bg(use_texture, &grid_texture);
|
||||
|
||||
int board[9][9] = {
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
};
|
||||
srand(time(NULL));
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
int r = (rand() % 9) + 1;
|
||||
board[i][j] = r;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
printf("%d | ", board[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
bool quit = false;
|
||||
while (!quit) {
|
||||
@@ -76,10 +128,17 @@ int main(int argc, char *argv[]) {
|
||||
quit = true;
|
||||
break;
|
||||
}
|
||||
} else if (xev.type == ButtonPress) {
|
||||
if (xev.xbutton.button == Button1) {
|
||||
int x = xev.xbutton.x;
|
||||
int y = xev.xbutton.y;
|
||||
// TOOD:
|
||||
printf("x: %d, y: %d\n", x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||
glClearColor(0.2, 0.2, 0.2, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
float transform[4][4] = {
|
||||
@@ -92,7 +151,9 @@ int main(int argc, char *argv[]) {
|
||||
if (!use_texture)
|
||||
draw_bg_grid_shader(grid_shader, vao, (float *)transform, width < height ? width : height);
|
||||
else
|
||||
draw_bg_grid_texture(grid_shader, vao, (float *)transform);
|
||||
draw_bg_grid_texture(grid_shader, vao, grid_texture, (float *)transform);
|
||||
/* draw_numbers(font_shader, font_vao, font_vbo, (float *)transform, board); */
|
||||
draw_win_overlay(font_shader, font_vao, font_vbo, (float *)transform);
|
||||
|
||||
glXSwapBuffers(display, window);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ void read_shader_file(const char *path, char **buf) {
|
||||
FILE *fp = fopen(path, "rb");
|
||||
|
||||
if (!fp) {
|
||||
printf("Could not open file %s", path);
|
||||
printf("Could not open file \"%s\"\n", path);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -20,12 +20,7 @@ void read_shader_file(const char *path, char **buf) {
|
||||
*buf = malloc(filesize + 1);
|
||||
(*buf)[filesize] = '\0';
|
||||
|
||||
|
||||
size_t bytes_read = fread(*buf, filesize, 1, fp);
|
||||
if (bytes_read != sizeof(**buf)) {
|
||||
printf("Failed to fully read file %s", path);
|
||||
exit(1);
|
||||
}
|
||||
fread(*buf, filesize, 1, fp);
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
@@ -82,7 +77,7 @@ void use_shader(Shader shader) {
|
||||
glUseProgram(shader.program);
|
||||
}
|
||||
|
||||
void set_mat4(Shader shader, const char *name, float *mat4) {
|
||||
void set_mat4f(Shader shader, const char *name, float *mat4) {
|
||||
int loc = glGetUniformLocation(shader.program, name);
|
||||
glUniformMatrix4fv(loc, 1, GL_FALSE, mat4);
|
||||
}
|
||||
@@ -91,3 +86,8 @@ void set_float(Shader shader, const char *name, float val) {
|
||||
int loc = glGetUniformLocation(shader.program, name);
|
||||
glUniform1f(loc, val);
|
||||
}
|
||||
|
||||
void set_vec3f(Shader shader, const char *name, float val1, float val2, float val3) {
|
||||
int loc = glGetUniformLocation(shader.program, name);
|
||||
glUniform3f(loc, val1, val2, val3);
|
||||
}
|
||||
|
||||
@@ -7,5 +7,6 @@ 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);
|
||||
void set_mat4f(Shader shader, const char *name, float *mat);
|
||||
void set_float(Shader shader, const char *name, float val);
|
||||
void set_vec3f(Shader shader, const char *name, float val1, float val2, float val3);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#version 330 core
|
||||
in vec2 TexCoords;
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform sampler2D text;
|
||||
uniform vec3 textColor;
|
||||
|
||||
void main() {
|
||||
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);
|
||||
FragColor = vec4(textColor, 1.0) * sampled;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec4 vertex;
|
||||
|
||||
out vec2 TexCoords;
|
||||
uniform mat4 transform;
|
||||
|
||||
void main() {
|
||||
gl_Position = transform * vec4(vertex.xy, 0.0, 1.0);
|
||||
TexCoords = vertex.zw;
|
||||
}
|
||||
@@ -47,3 +47,4 @@ void main()
|
||||
FragColor = vec4(240.f/255.f, 235.f/255.f, 227.f/255.f, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,8 @@ int init_x11(Display **display, Window *window) {
|
||||
colormap = XCreateColormap(*display, root, visual, AllocNone);
|
||||
|
||||
XSetWindowAttributes attributes;
|
||||
attributes.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | StructureNotifyMask;
|
||||
attributes.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
|
||||
StructureNotifyMask | ButtonPressMask;
|
||||
attributes.colormap = colormap;
|
||||
|
||||
*window = XCreateWindow(*display, root, 0, 0, win_width, win_height, 0,
|
||||
@@ -91,6 +92,10 @@ int init_x11(Display **display, Window *window) {
|
||||
|
||||
XWindowAttributes win_attrs;
|
||||
XGetWindowAttributes(*display, *window, &win_attrs);
|
||||
|
||||
// we enable blending for text
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glViewport(0, 0, win_attrs.width, win_attrs.height);
|
||||
resize_x11_window(*display, *window);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user