Browse Source

organize to utils and gui

master
Alexandros Theodotou 2 years ago
parent
commit
654bf4f1eb
Signed by: alex GPG Key ID: 022EAE42313D70F3
  1. 66
      inc/gui/gl.h
  2. 50
      inc/utils/pango.h
  3. 40
      inc/utils/ui.h
  4. 1
      meson.build
  5. 112
      src/gui/gl.c
  6. 20
      src/gui/meson.build
  7. 21
      src/main.c
  8. 6
      src/meson.build
  9. 21
      src/utils/meson.build
  10. 111
      src/utils/pango.c

66
inc/gui/gl.h

@ -0,0 +1,66 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Ion
*
* Ion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Ion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file
*
* OpenGL utils.
*/
#ifndef __GUI_GL_H__
#define __GUI_GL_H__
/**
* Texture info.
*/
typedef struct IonGlTexture
{
/** OpenGL texture ID. */
unsigned int id;
unsigned int width;
unsigned int height;
unsigned char * pixels;
} IonGlTexture;
/**
* Initialization during startup.
*/
void
ion_gl_init ();
/**
* Generate a texture with an ID for the given data
* of the given dimensions.
*/
IonGlTexture *
ion_gl_create_texture (
unsigned int width,
unsigned int height,
unsigned char * pixels);
/**
* Draws the texture in the given rectangle.
*/
void
ion_gl_draw_texture (
IonGlTexture * texture,
float x,
float y);
#endif

50
inc/utils/pango.h

@ -0,0 +1,50 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Ion
*
* Ion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Ion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file
*
* Pango utils.
*/
#ifndef __UTILS_PANGO_H__
#define __UTILS_PANGO_H__
typedef struct IonGlTexture IonGlTexture;
/**
* @addtogroup utils
*
* @{
*/
/**
* Renders the given text in the given font to a
* GL texture.
*/
IonGlTexture *
ion_pango_render_text_to_texture (
const char * font_descr,
const char * text);
/**
* @}
*/
#endif

40
inc/utils/ui.h

@ -0,0 +1,40 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Ion
*
* Ion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Ion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file
*
* UI utils.
*/
#ifndef __UTILS_UI_H__
#define __UTILS_UI_H__
/**
* Basic rectangle.
*/
typedef struct UiRectangle
{
float x;
float y;
float width;
float height;
} UiRectangle;
#endif

1
meson.build

@ -50,6 +50,7 @@ endforeach
ion_deps = [
dependency('glfw3'),
dependency('gl'),
dependency('pangocairo'),
]
subdir ('src')

112
src/gui/gl.c

@ -0,0 +1,112 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Ion
*
* Ion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Ion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <GLFW/glfw3.h>
#include <glib.h>
#include "gui/gl.h"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
/**
* Initialization during startup.
*/
void
ion_gl_init ()
{
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glDisable (GL_DEPTH_TEST);
glEnable (GL_BLEND);
/* enable multisampling (for antialiasing) */
glEnable(GL_MULTISAMPLE);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_TEXTURE_2D);
glViewport (0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -1, 1);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
/**
* Generate a texture with an ID for the given data
* of the given dimensions.
*/
IonGlTexture *
ion_gl_create_texture (
unsigned int width,
unsigned int height,
unsigned char * pixels)
{
IonGlTexture * texture =
calloc (1, sizeof (IonGlTexture));
glGenTextures (1, &texture->id);
glBindTexture (GL_TEXTURE_2D, texture->id);
glTexParameteri (
GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (
GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D (
GL_TEXTURE_2D, 0, GL_RGBA, width, height,
0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
texture->width = width;
texture->height = height;
return texture;
}
/**
* Draws the texture in the given rectangle.
*/
void
ion_gl_draw_texture (
IonGlTexture * texture,
float x,
float y)
{
/* Render a texture in immediate mode. */
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix ();
glBindTexture (GL_TEXTURE_2D, texture->id);
glColor3f (1.f, 1.0f, 1.0f);
glBegin (GL_QUADS);
glTexCoord2f (0.0f, 0.0f);
glVertex2f (x, y);
glTexCoord2f (1.0f, 0.0f);
glVertex2f (x + texture->width, y);
glTexCoord2f (1.0f, 1.0f);
glVertex2f (
x + texture->width, y + texture->height);
glTexCoord2f (0.0f, 1.0f);
glVertex2f (x, y + texture->height);
glEnd ();
glPopMatrix ();
}

20
src/gui/meson.build

@ -0,0 +1,20 @@
# Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
#
# This file is part of Ion
#
# Ion is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ion is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Ion. If not, see <https://www.gnu.org/licenses/>.
ion_srcs += files ([
'gl.c'
])

21
src/main.c

@ -19,9 +19,15 @@
#include <stdio.h>
#include <pango/pangocairo.h>
#include <GLFW/glfw3.h>
#include "gui/gl.h"
#include "utils/pango.h"
#define PROJECT_NAME "Ion"
#define FONT "Sans Bold 18"
#define TEXT "testτεστ"
int main (
int argc, char **argv)
@ -34,7 +40,9 @@ int main (
return -1;
}
/* Create a windowed mode window and its OpenGL context */
/* Create a windowed mode window and its OpenGL
* context */
glfwWindowHint (GLFW_SAMPLES, 4);
window =
glfwCreateWindow (
640, 480, PROJECT_NAME, NULL, NULL);
@ -47,12 +55,20 @@ int main (
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* init openGL */
ion_gl_init ();
IonGlTexture * texture =
ion_pango_render_text_to_texture (FONT, TEXT);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
ion_gl_draw_texture (texture, 0.f, 0.f);
/* Swap front and back buffers */
glfwSwapBuffers(window);
@ -60,6 +76,9 @@ int main (
glfwPollEvents();
}
/* Clean up */
glDeleteTextures (1, &texture->id);
glfwTerminate();
return 0;

6
src/meson.build

@ -16,8 +16,10 @@
# along with Ion. If not, see <https://www.gnu.org/licenses/>.
ion_main = files ('main.c')
ion_srcs = files ([
])
ion_srcs = files ([])
subdir ('gui')
subdir ('utils')
ion_exe = executable(
'ion',

21
src/utils/meson.build

@ -0,0 +1,21 @@
# Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
#
# This file is part of Ion
#
# Ion is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ion is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Ion. If not, see <https://www.gnu.org/licenses/>.
ion_srcs += files ([
'pango.c'
])

111
src/utils/pango.c

@ -0,0 +1,111 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Ion
*
* Ion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Ion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <pango/pangocairo.h>
#include "gui/gl.h"
#include "utils/pango.h"
/**
* Renders the given text in the given font to a
* GL texture.
*/
IonGlTexture *
ion_pango_render_text_to_texture (
const char * font_descr,
const char * text)
{
/*
* Create a cairo context for PangoLayout objects.
*
* The pango layout needs a context in order to
* function. We use cairo_image_surface_create()
* with dimensions of 0 x 0 because we wont
* actually be rendering to this surface.
* Instead, we will layout our text
* and use create_cairo_context to build a context
* with a surface that is the size of the rendered
* text.
*
* Cairo uses reference counting for dynamically
* allocated objects, so we need to call
* cairo_surface_destroy() when were done with the
* temporary surface. The context still maintains a
* reference to the surface, so the memory for the
* surface will not be freed until the context is.
*/
cairo_surface_t * temp_surface =
cairo_image_surface_create (
CAIRO_FORMAT_ARGB32, 0, 0);
cairo_t * layout_cr = cairo_create (temp_surface);
cairo_surface_destroy (temp_surface);
/* Create a PangoLayout, set the font and text */
PangoLayout * layout =
pango_cairo_create_layout (layout_cr);
pango_layout_set_text (layout, text, -1);
/* Load the font */
PangoFontDescription * desc =
pango_font_description_from_string (font_descr);
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);
/* get text dimensions (divide by pango scale to
* get dimensions in pixels) */
int width, height;
pango_layout_get_size (layout, &width, &height);
width /= PANGO_SCALE;
height /= PANGO_SCALE;
/* Get text dimensions and create a context to
* render to (will render on the raw surface) */
int stride =
cairo_format_stride_for_width (
CAIRO_FORMAT_ARGB32, width);
unsigned char* surface_data =
calloc (
stride * height, sizeof (unsigned char));
cairo_surface_t * surface =
cairo_image_surface_create_for_data (
surface_data, CAIRO_FORMAT_ARGB32,
width, height, stride);
cairo_t * cr =
cairo_create (surface);
/* Render */
cairo_set_source_rgba (cr, 1, 1, 1, 1);
pango_cairo_show_layout (cr, layout);
/* generate an openGL texture ID for the rendered
* data */
IonGlTexture * texture =
ion_gl_create_texture (
width, height, surface_data);
/* Clean up */
free (surface_data);
g_object_unref (layout);
cairo_destroy (layout_cr);
cairo_destroy (cr);
cairo_surface_destroy (surface);
/* return the texture */
return texture;
}
Loading…
Cancel
Save