GoldFish Engine
Quite simple and lightweight game engine
Loading...
Searching...
No Matches
gf_graphic_common.c
1#define GF_EXPOSE_DRAW
2#define GF_EXPOSE_TEXTURE
3#define GF_EXPOSE_FONT
4
5#include <gf_pre.h>
6
7/* External library */
8
9/* Interface */
10#include <gf_graphic.h>
11
12/* Engine */
13#include <gf_draw.h>
14#include <gf_font.h>
15
16/* Standard */
17#include <string.h>
18
19void gf_graphic_fill_rect(gf_draw_t* draw, double x, double y, double w, double h, gf_graphic_color_t color) { gf_graphic_fill_polygon(draw, color, GF_GRAPHIC_2D, 4, x, y, x, y + h, x + w, y + h, x + w, y); }
20
21void gf_graphic_text(gf_draw_t* draw, double x, double y, double size, const char* text, gf_graphic_color_t color) {
22 int i;
23 double mx = 0;
24 gf_font_glyph_t* glyph;
25 double zoom = 0;
26 if(draw->font != NULL) {
27 zoom = size / draw->font->bbox.height;
28 for(i = 0; text[i] != 0; i++) {
29 if((glyph = gf_font_get(draw->font, text[i])) != NULL) {
30 double fax = glyph->bbox.width;
31 double fay = glyph->bbox.height;
32 double fx = glyph->bbox.x;
33 double fy = (draw->font->bbox.height + draw->font->bbox.y) - (glyph->bbox.height + glyph->bbox.y);
34 gf_graphic_draw_texture_2d(draw, x + mx + fx * zoom, y + fy * zoom, zoom * fax, zoom * fay, glyph->texture, color);
35 mx += zoom * glyph->dwidth[0];
36 }
37 }
38 }
39}
40
41double gf_graphic_text_width(gf_draw_t* draw, double size, const char* text) {
42 int i;
43 double mx = 0;
44 gf_font_glyph_t* glyph;
45 double zoom = 0;
46 if(draw->font != NULL) {
47 zoom = size / draw->font->bbox.height;
48 for(i = 0; text[i] != 0; i++) {
49 if((glyph = gf_font_get(draw->font, text[i])) != NULL) {
50 mx += zoom * glyph->dwidth[0];
51 }
52 }
53 }
54 return mx;
55}
56
57void gf_graphic_draw_texture_2d(gf_draw_t* draw, double x, double y, double w, double h, gf_texture_t* texture, gf_graphic_color_t color) {
58 if(texture != NULL) gf_graphic_draw_texture_polygon(draw, texture, color, GF_GRAPHIC_2D, 4, 0.0, 0.0, x, y, 0.0, 1.0, x, y + h, 1.0, 1.0, x + w, y + h, 1.0, 0.0, x + w, y);
59}
Drawing interface.
gf_font_glyph_t * gf_font_get(gf_font_t *font, int code)
Get glyph.
Definition gf_font.c:20
Graphic interface.
void gf_graphic_draw_texture_2d(gf_draw_t *draw, double x, double y, double w, double h, gf_texture_t *texture, gf_graphic_color_t color)
Draw rectangle with texture.
void gf_graphic_draw_texture_polygon(gf_draw_t *draw, gf_texture_t *texture, gf_graphic_color_t color, int dim, int npair,...)
Draw polygon with texture.
Definition gf_graphic.c:28
void gf_graphic_fill_rect(gf_draw_t *draw, double x, double y, double w, double h, gf_graphic_color_t color)
Draw filled rectangle.
void gf_graphic_fill_polygon(gf_draw_t *draw, gf_graphic_color_t color, int dim, int npair,...)
Draw polygon.
Definition gf_graphic.c:30
void gf_graphic_text(gf_draw_t *draw, double x, double y, double size, const char *text, gf_graphic_color_t color)
Draw text.
double gf_graphic_text_width(gf_draw_t *draw, double size, const char *text)
Calculate text width.
#define GF_GRAPHIC_2D
Dimension parameter for 2D.
Definition gf_graphic.h:26
Required headers before anything.
Drawing interface.
Definition draw.h:108
gf_font_t * font
Current font.
Definition draw.h:108
int y
Y coord.
Definition font.h:47
int width
Width.
Definition font.h:47
int x
X coord.
Definition font.h:47
int height
Height.
Definition font.h:47
Glyph.
Definition font.h:75
int dwidth[2]
Device width.
Definition font.h:75
gf_font_bbox_t bbox
Bounding box.
Definition font.h:75
gf_texture_t * texture
Texture.
Definition font.h:75
gf_font_bbox_t bbox
Bounding box.
Definition font.h:95
Texture.
Definition texture.h:49