Browse code

Create main window using boxui

Dario Rodriguez authored on 28/12/2023 12:16:42
Showing 10 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,55 @@
1
+CC=gcc
2
+CFLAGS=-Wall -g -I/usr/include/SDL2 -DLINUX
3
+LDFLAGS=-lSDL2_ttf -lSDL2
4
+
5
+all: annotator annotator.exe annotator.html
6
+
7
+# common
8
+
9
+notosans_regular_ttf.c: fonts/NotoSans-Regular.ttf
10
+	(cd fonts && xxd -i NotoSans-Regular.ttf | tr A-Z a-z ) > notosans_regular_ttf.c
11
+
12
+notosans_regular_ttf.h: fonts/NotoSans-Regular.ttf
13
+	(cd fonts && xxd -i NotoSans-Regular.ttf | sed "s/^\(.*[^ ]\) *=.*/extern \1\;/g" | grep extern | tr A-Z a-z ) > notosans_regular_ttf.h
14
+
15
+# boxui.o: linux
16
+
17
+boxui.o: boxui.c boxui.h notosans_regular_ttf.c notosans_regular_ttf.h
18
+
19
+# boxui-windows.o: windows
20
+
21
+boxui-windows.o: boxui.c boxui.h notosans_regular_ttf.c notosans_regular_ttf.h
22
+	toolchain-zig/compile.sh boxui.c -c -o boxui-windows.o
23
+
24
+# boxui-html.o: html5
25
+
26
+boxui-html.o: boxui.c boxui.h notosans_regular_ttf.c notosans_regular_ttf.h
27
+	sh -c ". toolchain-emscripten/env.sh ; emcc -g -Wall -s USE_SDL=2 -s USE_SDL_TTF=2 boxui.c -c -o boxui-html.o"
28
+
29
+# annotator: linux
30
+
31
+annotator.o: annotator.c boxui.h
32
+
33
+annotator: boxui.o annotator.o
34
+	gcc annotator.o boxui.o -o annotator -lSDL2_ttf -lSDL2
35
+
36
+# annotator: windows
37
+
38
+annotator-windows.o: annotator.c boxui.h
39
+	toolchain-zig/compile.sh annotator.c -c -o annotator-windows.o
40
+
41
+annotator.exe: annotator-icon_256x256.png boxui-windows.o annotator-windows.o
42
+	toolchain-zig/link.sh -icon annotator-icon_256x256.png annotator.exe boxui-windows.o annotator-windows.o
43
+
44
+# annotator: html5
45
+
46
+annotator-html.o: annotator.c
47
+	sh -c ". toolchain-emscripten/env.sh ; emcc -g -Wall -s USE_SDL=2 -s USE_SDL_TTF=2 annotator.c -c -o annotator-html.o"
48
+
49
+annotator.html: boxui-html.template boxui-html.o annotator-html.o
50
+	sh -c ". toolchain-emscripten/env.sh ; emcc annotator-html.o boxui-html.o -o annotator.html -s USE_SDL=2 -s USE_SDL_TTF=2 && sed s/PROGRAMNAME/annotator/g < boxui-html.template > annotator.html && zip -pr annotator-html.zip annotator.html annotator.js annotator.wasm"
51
+
52
+clean:
53
+	rm -f *.o *.js *.html *.wasm *.exe \
54
+ notosans_regular_ttf.c notosans_regular_ttf.h \
55
+ annotator annotator.exe annotator-html.zip
0 56
new file mode 100644
1 57
Binary files /dev/null and b/annotator-icon_256x256.png differ
2 58
new file mode 100644
3 59
Binary files /dev/null and b/annotator-icon_256x256.psd differ
4 60
new file mode 100644
... ...
@@ -0,0 +1,139 @@
1
+/*
2
+ * annotator.c
3
+ *
4
+ *  Annotates a image file containg a piano music sheet with the help of a human
5
+ * for use in the main program.
6
+ *
7
+ * Author: Dario Rodriguez <antartica@whereismybit.com>
8
+ * Please see the file LICENSE.txt in the sources root directory
9
+ */
10
+
11
+#ifdef EMSCRIPTEN
12
+#include <emscripten.h>
13
+#endif
14
+#include <stdio.h>
15
+#include <stdlib.h>
16
+#include <unistd.h>
17
+#include <string.h>
18
+#include <time.h>
19
+#include <signal.h>
20
+#include "boxui.h"
21
+
22
+#define DEFAULT_SCREENW 640
23
+#define DEFAULT_SCREENH 480
24
+
25
+typedef struct mydata_t {
26
+        int flag_exit;
27
+        boxui_t *boxui;
28
+} mydata_t;
29
+
30
+volatile int flag_sigint;
31
+static void sigint(int n);
32
+static int install_signal(int n,void (*f)(int));
33
+
34
+mydata_t *mydata_init(void);
35
+void mydata_free(mydata_t *mydata);
36
+int main_loop_iteration(mydata_t *mydata);
37
+
38
+int
39
+main(int argc, char *argv[])
40
+{
41
+        mydata_t *mydata;
42
+        if((mydata=mydata_init())==NULL) {
43
+                DEBUG_FPRINTF((stderr,"Couldn't init\n"));
44
+                return(-1); /* couldn't init */
45
+        }
46
+        flag_sigint=0;
47
+        install_signal(SIGINT,sigint);
48
+        while(flag_sigint==0 && mydata->flag_exit==0 && mydata->boxui!=NULL) {
49
+#ifdef EMSCRIPTEN
50
+                emscripten_set_main_loop_arg((void (*)(void *))main_loop_iteration,(void *) mydata,boxui_getfps(mydata->boxui),1);
51
+#else
52
+                main_loop_iteration(mydata);
53
+#endif
54
+        }
55
+        mydata_free(mydata),mydata=NULL;
56
+        return(0);
57
+}
58
+
59
+static int
60
+install_signal(int n,void (*f)(int))
61
+{
62
+#ifndef LINUX
63
+        return(-1);
64
+#else
65
+        struct sigaction sact;
66
+        sact.sa_handler=f;
67
+        sigemptyset(&sact.sa_mask);
68
+        sact.sa_flags=0;
69
+        return(sigaction(n,&sact,0));
70
+#endif
71
+}
72
+
73
+static void
74
+sigint(int n)
75
+{
76
+        flag_sigint++;
77
+}
78
+
79
+mydata_t *
80
+mydata_init(void)
81
+{
82
+        boxui_t *boxui;
83
+        mydata_t *mydata;
84
+        char *p;
85
+        if((mydata=malloc(sizeof(mydata_t)))==NULL)
86
+                return(NULL);
87
+        memset(mydata,0,sizeof(mydata_t));
88
+        if((mydata->boxui=boxui=boxui_init("Presto! Sight-Reading - Annotator",DEFAULT_SCREENW,DEFAULT_SCREENH,0))==NULL
89
+          || boxui_setuserptr(boxui,mydata)!=0) {
90
+                mydata_free(mydata),mydata=NULL;
91
+                return(NULL);
92
+        }
93
+        /* initial UI config */
94
+        p=boxui_add(boxui,_F,_L,"label:.l","-text \"(Here goes the music sheet)\"");
95
+        boxui_pack(boxui,_F,_L,p,"-side top -fill both -expand true");
96
+        return(mydata);
97
+}
98
+
99
+void
100
+mydata_free(mydata_t *mydata)
101
+{
102
+        boxui_t *boxui;
103
+        if(mydata==NULL)
104
+                return;
105
+        if((boxui=mydata->boxui)!=NULL) {
106
+                boxui_setuserptr(boxui,NULL);
107
+                boxui_free(boxui),boxui=NULL,mydata->boxui=NULL;
108
+        }
109
+        free(mydata),mydata=NULL;
110
+        return;
111
+}
112
+
113
+int
114
+main_loop_iteration(mydata_t *mydata)
115
+{
116
+        boxui_t *boxui;
117
+        SDL_Event event;
118
+        int keycode,neww,newh;
119
+        char *actionstr,*actionparams;
120
+        if(mydata==NULL || (boxui=mydata->boxui)==NULL)
121
+                return(-1);
122
+        boxui_tick(boxui);
123
+        boxui_refreshevents(boxui);
124
+        while(boxui_getevent(boxui,&event)==0) {
125
+                /* standard events */
126
+                if(boxui_event_isquit(boxui,&event))
127
+                        mydata->flag_exit=1;
128
+                if(boxui_event_iskeydown(boxui,&event,&keycode) && keycode==SDLK_ESCAPE)
129
+                        mydata->flag_exit=1;
130
+                if(boxui_event_isresize(boxui,&event,&neww,&newh))
131
+                        boxui_resize(boxui,neww,newh);
132
+        }
133
+        while((actionstr=boxui_getactionstr(boxui,&actionparams))!=NULL) {
134
+                if(strcmp(actionstr,"exit")==0)
135
+                        mydata->flag_exit=1;
136
+        }
137
+        return(0);
138
+}
139
+
0 140
new file mode 120000
... ...
@@ -0,0 +1 @@
1
+../boxui/boxui-html.template
0 2
\ No newline at end of file
1 3
new file mode 120000
... ...
@@ -0,0 +1 @@
1
+../boxui/boxui.c
0 2
\ No newline at end of file
1 3
new file mode 120000
... ...
@@ -0,0 +1 @@
1
+../boxui/boxui.h
0 2
\ No newline at end of file
1 3
new file mode 120000
... ...
@@ -0,0 +1 @@
1
+../boxui/fonts
0 2
\ No newline at end of file
1 3
new file mode 120000
... ...
@@ -0,0 +1 @@
1
+../boxui/toolchain-emscripten
0 2
\ No newline at end of file
1 3
new file mode 120000
... ...
@@ -0,0 +1 @@
1
+../boxui/toolchain-zig
0 2
\ No newline at end of file