Skip to content

Commit

Permalink
Modify font editor
Browse files Browse the repository at this point in the history
Modify some contents in font editor

1. Move the headers from twin-fedit.h to twin-efdit.c.

2. Rewrite some commnets in C-style.

3. Include bool.h to replace int type with bool type for exit_window

4. Let draw_char(c) be called befor SDL_PollEvent(&event)
  • Loading branch information
jouae committed Nov 23, 2024
1 parent 720241d commit 7231d62
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion tools/font-edit/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TARGET = twin-fedit

CFLAGS = $(shell pkg-config --cflags cairo) $(shell sdl2-config --cflags) -g -Wall
CFLAGS = $(shell pkg-config --cflags cairo) $(shell sdl2-config --cflags) -g -Wall
LIBS = $(shell pkg-config --libs cairo) $(shell sdl2-config --libs)

OBJS = \
Expand Down
56 changes: 28 additions & 28 deletions tools/font-edit/twin-fedit.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

#include "twin-fedit.h"

#include <SDL2/SDL.h>
#include <SDL2/SDL_keycode.h>
#include <cairo.h>
#include <stdbool.h>

static SDL_Window *window;
static cairo_t *cr;
static cairo_surface_t *surface;
Expand All @@ -37,7 +42,7 @@ static int offsets[1024];
* if the value is 0, the windown lives,
* otherwise the windown quit.
*/
static int exit_window = 0;
static bool exit_window = 0;

static int init(int argc, char **argv)
{
Expand All @@ -46,7 +51,7 @@ static int init(int argc, char **argv)
return 0;
}

window = SDL_CreateWindow("Twin Fedit", SDL_WINDOWPOS_CENTERED,
window = SDL_CreateWindow("Font Editor", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height,
SDL_WINDOW_SHOWN);
if (!window) {
Expand Down Expand Up @@ -501,54 +506,52 @@ static void play(char_t *c)
SDL_StopTextInput();

SDL_Event event;
int quit_state = 0;
SDL_Keycode key_event;

/* keep track of the selected spline */
cmd_t *spline = NULL;
while (!quit_state) {
draw_char(c);

for(;;){
while (SDL_PollEvent(&event)) {
switch (event.type) {
/* If SDL event is detected */
case SDL_QUIT:
/* Click the "X" on the top of screen to exit the program */
quit_state = 1;
exit_window = 1;
break;
return;
case SDL_KEYDOWN:
/* If any key event is detected */
SDL_Keycode key_event = event.key.keysym.sym;
key_event = event.key.keysym.sym;

switch (key_event) {
case SDLK_q:
/* Press "q" key to exit play()
/* To exit play()
* This allows the user to display the next character.
*/
quit_state = 1;
break;
return;
case SDLK_ESCAPE:
/* Press "ESC" key to quit the program */
quit_state = 1;
/* To quit the program */
exit_window = 1;
break;
return;
case SDLK_s:
/* Press "s" key to split the command between first and last
*/
/* To split the command between first char and last char */
if (c->first && c->last) {
split(c, c->first, c->last);
}
break;
case SDLK_u:
/* Press "u" key to undo the last operation */
/* To undo the last operation */
undo(c);
break;
case SDLK_f:
/* Press "f" key to replace with spline between first and
* last */
/* To replace with spline between first and last */
if (c->first && c->last) {
replace_with_spline(c, c->first, c->last);
}
break;
case SDLK_d:
/* Press "d" key to delete the first command */
/* To delete the first command */
if (c->first) {
delete (c, c->first);
}
Expand All @@ -569,25 +572,22 @@ static void play(char_t *c)
}
break;
}
draw_char(
c); // Ensure the content is redrawn after every key press
/* Ensure the content is redrawn after every key press */
break;
/* End if key event detected */
/* End if key event detected */
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_EXPOSED)
draw_char(c);
break;
break;
case SDL_MOUSEBUTTONDOWN:
/* Redraw the content after mouse button interaction */
button(c, &event.button);
draw_char(
c); // Redraw the content after mouse button interaction
break;
break;
/* End if SDL event detected */
}
/* End if SDL event detected */
}

// Ensure the window surface is updated
/* Ensure the SDL window surface is updated */
SDL_UpdateWindowSurface(window);
}
}
Expand Down
3 changes: 0 additions & 3 deletions tools/font-edit/twin-fedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
#ifndef _TWIN_FEDIT_H_
#define _TWIN_FEDIT_H_

#include <SDL2/SDL.h>
#include <SDL2/SDL_keycode.h>
#include <cairo.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down

0 comments on commit 7231d62

Please sign in to comment.