ソースを参照

Drawing char ontop of cursor

Vinicius Teshima 1 年間 前
コミット
16f2409e1b
3 ファイル変更45 行追加19 行削除
  1. 43 13
      src/app.h
  2. 2 0
      src/font.h
  3. 0 6
      src/window.h

+ 43 - 13
src/app.h

@@ -24,6 +24,9 @@ struct app {
 
 struct app app_create(const char *win_title);
 
+void app_set_text_color(struct app app, uint32_t color);
+uint32_t app_get_text_color(struct app app);
+
 void app_render_cursor_in_pos(struct app app, struct vec2 pos, uint32_t color);
 void app_render_cursor(struct app app, uint32_t color);
 void app_render_char(struct app app, const char c, struct vec2 pos,
@@ -32,9 +35,15 @@ void app_render_char(struct app app, const char c, struct vec2 pos,
 void app_render_text(struct app app, const char *text, size_t text_size,
 		     struct vec2 pos, uint32_t color, double scale);
 
-
+#define IMP
 #if defined(APP_IMP) || defined(IMP)
 
+#define UNHEX_COLOR(hex)			\
+	(uint8_t) ((hex >> 24) & 0xFF),	\
+	(uint8_t) ((hex >> 16) & 0xFF),	\
+	(uint8_t) ((hex >> 8 ) & 0xFF),	\
+	(uint8_t) ((hex >> 0 ) & 0xFF)
+
 #include "sc.h"
 
 struct app
@@ -73,9 +82,7 @@ app_render_char(struct app app, const char c, struct vec2 pos, double scale)
 }
 
 void
-app_render_text(struct app app, const char *text, size_t text_size,
-		struct vec2 pos, uint32_t color, double scale)
-{
+app_set_text_color(struct app app, uint32_t color) {
 	uint8_t r = (uint8_t) ((color >> 24) & 0xFF);
 	uint8_t g = (uint8_t) ((color >> 16) & 0xFF);
 	uint8_t b = (uint8_t) ((color >> 8 ) & 0xFF);
@@ -83,6 +90,26 @@ app_render_text(struct app app, const char *text, size_t text_size,
 
 	SCE(SDL_SetTextureColorMod(app.font.tex, r, g, b));
 	SCE(SDL_SetTextureAlphaMod(app.font.tex, a));
+}
+
+uint32_t
+app_get_text_color(struct app app) {
+	uint8_t r = 0;
+	uint8_t g = 0;
+	uint8_t b = 0;
+	uint8_t a = 0;
+
+	SCE(SDL_GetTextureColorMod(app.font.tex, &r, &g, &b));
+	SCE(SDL_GetTextureAlphaMod(app.font.tex, &a));
+
+	return (r << 24) | (g << 16) | (b << 8) | (a << 0);
+}
+
+void
+app_render_text(struct app app, const char *text, size_t text_size,
+		struct vec2 pos, uint32_t color, double scale)
+{
+	app_set_text_color(app, color);
 
 	struct vec2 pen = pos;
 	size_t i = 0;
@@ -117,22 +144,25 @@ app_render_cursor_in_pos(struct app app, struct vec2 pos, uint32_t color)
 
 	SCE(SDL_SetRenderDrawColor(app.rdr, UNHEX_COLOR(color)));
 	SCE(SDL_RenderFillRect(app.rdr, &rect));
+
+	if ( app.buf.cur < app.buf.data.size ) {
+		uint32_t old_color = app_get_text_color(app);
+		app_set_text_color(app, ((~color) | 0xFF));
+		char c = app.buf.data.items[app.buf.cur];
+		app_render_char(app, c, pos, app.font.scale);
+		app_set_text_color(app, old_color);
+	}
 }
 
 void
 app_render_cursor(struct app app, uint32_t color)
 {
-	SDL_Rect rect = {0
-		/* .x = (int32_t) (app.buf.cur.x */
-		/* 		* (uint64_t) (app.font.ch_w * app.font.scale)), */
-		/* .y = (int32_t) (app.buf.cur.y */
-		/* 		* (uint64_t) (app.font.ch_h * app.font.scale)), */
-		/* .w = (int32_t) (app.font.ch_w * app.font.scale), */
-		/* .h = (int32_t) (app.font.ch_h * app.font.scale) */
+	struct vec2 pos = {
+		.x = ((double)(app.buf.cur) * (app.font.ch_w * app.font.scale)),
+		.y = ((double)(app.buf.cur) * (app.font.ch_h * app.font.scale))
 	};
 
-	SCE(SDL_SetRenderDrawColor(app.rdr, UNHEX_COLOR(color)));
-	SCE(SDL_RenderFillRect(app.rdr, &rect));
+	app_render_cursor_in_pos(app, pos, color);
 }
 
 #endif /* defined(APP_IMP) || defined(IMP) */

+ 2 - 0
src/font.h

@@ -36,6 +36,8 @@ font_create(SDL_Renderer *rdr, const char *filepath,
 	f.sur = surface_from_file(filepath);
 	SCP(f.sur);
 
+	SDL_SetColorKey(f.sur, SDL_TRUE, 0x00000000);
+
 	f.tex = SDL_CreateTextureFromSurface(rdr, f.sur);
 	SCP(f.tex);
 

+ 0 - 6
src/window.h

@@ -9,12 +9,6 @@ struct window {
 	int32_t w;
 };
 
-#define UNHEX_COLOR(hex)			\
-	(uint8_t) ((hex >> 24) & 0xFF),	\
-	(uint8_t) ((hex >> 16) & 0xFF),	\
-	(uint8_t) ((hex >> 8 ) & 0xFF),	\
-	(uint8_t) ((hex >> 0 ) & 0xFF)
-
 #define WINDOW_UP_SIZE(win) SDL_GetWindowSize(win.ptr, &win.w, &win.h)
 
 struct window window_create(const char *win_title);