aboutsummaryrefslogtreecommitdiff
path: root/render
diff options
context:
space:
mode:
authorjacopo grandi <jak.sk8@hotmail.it>2021-02-18 14:14:23 +0100
committerjacopo grandi <jak.sk8@hotmail.it>2021-02-18 14:14:23 +0100
commit5f0fce4191309e9526b7109a0d87c092ce6a4193 (patch)
tree105257f876551814aa74a0760ec116bd1bf307a5 /render
parentead78d51e662057467b79d3a65b20c4ba83cbf07 (diff)
main
Diffstat (limited to 'render')
-rw-r--r--render/button.c21
-rw-r--r--render/button.h16
-rw-r--r--render/graphicsettings.h8
-rw-r--r--render/render_text.c48
-rw-r--r--render/render_text.h18
5 files changed, 111 insertions, 0 deletions
diff --git a/render/button.c b/render/button.c
new file mode 100644
index 0000000..eb96f59
--- /dev/null
+++ b/render/button.c
@@ -0,0 +1,21 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <button.h>
+
+#include <intersect.h>
+
+bool mouse_in_button (float pt[], txtd *t, button *b) {
+ int width = get_text_width(b->txt, t);
+ float size[2] = { width+b->pad*2, 10+b->pad*2 };
+ if (pt_rect(pt, b->pos, size)) return true;
+ return false;
+}
+
+void render_button (SDL_Renderer* rend, txtd *t, button *b) {
+ int width = get_text_width(b->txt, t);
+ SDL_Rect rect = { b->pos[0], b->pos[1], width+b->pad*2, 10+b->pad*2 };
+ SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);
+ SDL_RenderDrawRect(rend, &rect);
+ float offpad[2] = { b->pos[0]+b->pad, b->pos[1]+b->pad };
+ render_text(rend, b->txt, offpad, t);
+} \ No newline at end of file
diff --git a/render/button.h b/render/button.h
new file mode 100644
index 0000000..b8d10b6
--- /dev/null
+++ b/render/button.h
@@ -0,0 +1,16 @@
+#ifndef BUTTON_H
+#define BUTTON_H
+
+#include <SDL.h>
+#include <render_text.h>
+
+typedef struct {
+ char txt[32];
+ int pad;
+ float pos[2];
+} button;
+
+bool mouse_in_button (float pt[], txtd *t, button *b);
+void render_button (SDL_Renderer* rend, txtd *t, button *b);
+
+#endif \ No newline at end of file
diff --git a/render/graphicsettings.h b/render/graphicsettings.h
new file mode 100644
index 0000000..53a83c2
--- /dev/null
+++ b/render/graphicsettings.h
@@ -0,0 +1,8 @@
+#ifndef GRAPHICSETTINGS_H
+#define GRAPHICSETTINGS_H
+
+typedef struct {
+ int resx, resy;
+} graphic_settings;
+
+#endif \ No newline at end of file
diff --git a/render/render_text.c b/render/render_text.c
new file mode 100644
index 0000000..67a13e2
--- /dev/null
+++ b/render/render_text.c
@@ -0,0 +1,48 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <render_text.h>
+
+void char_width_init (int *char_width) {
+ for (int i=0; i<128; char_width[i++]=5);
+ char_width[','] = 1; char_width['-'] = 3; char_width['.'] = 1;
+ char_width['/'] = 4; char_width['!'] = 1;
+ char_width[':'] = 1; char_width[';'] = 1;
+ char_width['<'] = 3; char_width['>'] = 3; char_width['='] = 4;
+ char_width['I'] = 1;
+ char_width['f'] = 4; char_width['i'] = 1; char_width['j'] = 3;
+ char_width['l'] = 1; char_width['k'] = 4; char_width['t'] = 4;
+}
+
+int get_text_width (char str[], txtd *t) {
+ int width = 0;
+ for (int i=0; str[i]!='\0'; i++) {
+ width += t->cw[str[i]];
+ if (str[i+1]!='\0') width++;
+ }
+ return width;
+}
+
+void render_text (SDL_Renderer* gRenderer, char str[], float off[], txtd *t)
+{
+ int width = 0;
+ for (int i=0; str[i]!='\0'; i++) {
+ int char_i = str[i];
+ SDL_Rect srcRect = { (char_i%32)*6+1, (char_i/32)*12+1, 5, 11 };
+ SDL_Rect dstRect = { off[0]+width, off[1], 5, 11 };
+ SDL_RenderCopy(gRenderer, t->tex, &srcRect, &dstRect);
+ width += t->cw[char_i]+1;
+ }
+}
+
+void render_text_scaled (SDL_Renderer* gRenderer, char str[],
+ float off[], txtd *t, float scale)
+{
+ int width = 0;
+ for (int i=0; str[i]!='\0'; i++) {
+ int char_i = str[i];
+ SDL_Rect srcRect = { (char_i%32)*6+1, (char_i/32)*12+1, 5, 11 };
+ SDL_Rect dstRect = { off[0]+width, off[1], 5*scale, 11*scale };
+ SDL_RenderCopy(gRenderer, t->tex, &srcRect, &dstRect);
+ width += t->cw[char_i]*scale+1*scale;
+ }
+} \ No newline at end of file
diff --git a/render/render_text.h b/render/render_text.h
new file mode 100644
index 0000000..e7474ff
--- /dev/null
+++ b/render/render_text.h
@@ -0,0 +1,18 @@
+#ifndef RENDER_TEXT_H
+#define RENDER_TEXT_H
+
+#include <SDL.h>
+
+typedef struct {
+ SDL_Texture *tex;
+ int cw[128];
+} txtd;
+
+void char_width_init (int *char_width);
+int get_text_width (char str[], txtd *t);
+
+void render_text (SDL_Renderer* gRenderer, char str[], float off[], txtd *t);
+void render_text_scaled (SDL_Renderer* gRenderer, char str[],
+ float off[], txtd *t, float scale);
+
+#endif \ No newline at end of file