9dfedc73eb7e391c92710ac80ec990a47737b9d3
[retroray] / src / scr_mod.c
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include "gaw/gaw.h"
19 #include "app.h"
20 #include "rtk.h"
21
22 enum {
23         TBN_NEW, TBN_OPEN, TBN_SAVE, TBN_SEP1,
24         TBN_SEL, TBN_MOVE, TBL_ROT, TBN_SCALE, TBN_SEP2,
25         TBL_ADD, TBL_RM, TBN_SEP3,
26         TBN_MTL, TBN_REND, TBL_VIEWREND, TBN_SEP4, TBL_CFG,
27
28         NUM_TOOL_BUTTONS
29 };
30 static const char *tbn_icon_name[] = {
31         "new", "open", "save", 0,
32         "sel", "move", "rot", "scale", 0,
33         "add", "remove", 0,
34         "mtl", "rend", "viewrend", 0, "cfg"
35 };
36 static int tbn_icon_pos[][2] = {
37         {0,0}, {16,0}, {32,0}, {-1,-1},
38         {48,0}, {64,0}, {80,0}, {96,0}, {-1,-1},
39         {112,0}, {112,16}, {-1,-1},
40         {48,16}, {64,16}, {80,16}, {-1,-1}, {96,16}
41 };
42 static rtk_icon *tbn_icons[NUM_TOOL_BUTTONS];
43
44 #define TOOLBAR_HEIGHT  26
45
46
47 static int mdl_init(void);
48 static void mdl_destroy(void);
49 static int mdl_start(void);
50 static void mdl_stop(void);
51 static void mdl_display(void);
52 static void mdl_reshape(int x, int y);
53 static void mdl_keyb(int key, int press);
54 static void mdl_mouse(int bn, int press, int x, int y);
55 static void mdl_motion(int x, int y);
56
57 static void draw_grid(void);
58
59
60 struct app_screen scr_model = {
61         "modeller",
62         mdl_init, mdl_destroy,
63         mdl_start, mdl_stop,
64         mdl_display, mdl_reshape,
65         mdl_keyb, mdl_mouse, mdl_motion
66 };
67
68 static rtk_widget *toolbar;
69 static rtk_iconsheet *icons;
70
71 static float cam_theta, cam_phi = 20, cam_dist = 8;
72
73
74 static int mdl_init(void)
75 {
76         int i;
77         rtk_widget *w;
78
79         if(!(icons = rtk_load_iconsheet("data/icons.png"))) {
80                 errormsg("failed to load iconsheet\n");
81                 return -1;
82         }
83         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
84                 if(tbn_icon_name[i]) {
85                         tbn_icons[i] = rtk_define_icon(icons, tbn_icon_name[i],
86                                         tbn_icon_pos[i][0], tbn_icon_pos[i][1], 16, 16);
87                 } else {
88                         tbn_icons[i] = 0;
89                 }
90         }
91
92         if(!(toolbar = rtk_create_window(0, "toolbar", 0, 0, win_width, TOOLBAR_HEIGHT))) {
93                 return -1;
94         }
95         rtk_win_layout(toolbar, RTK_HBOX);
96
97         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
98                 if(!tbn_icons[i]) {
99                         rtk_create_separator(toolbar);
100                 } else {
101                         if(!(w = rtk_create_iconbutton(toolbar, tbn_icons[i], 0))) {
102                                 return -1;
103                         }
104                 }
105         }
106         return 0;
107 }
108
109 static void mdl_destroy(void)
110 {
111         rtk_free_iconsheet(icons);
112 }
113
114 static int mdl_start(void)
115 {
116         gaw_clear_color(0.125, 0.125, 0.125, 1);
117         return 0;
118 }
119
120 static void mdl_stop(void)
121 {
122 }
123
124 static void mdl_display(void)
125 {
126         gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
127
128         rtk_draw_widget(toolbar);
129
130         gaw_viewport(0, TOOLBAR_HEIGHT, win_width, win_height - TOOLBAR_HEIGHT);
131
132         gaw_matrix_mode(GAW_MODELVIEW);
133         gaw_load_identity();
134         gaw_translate(0, 0, -cam_dist);
135         gaw_rotate(cam_phi, 1, 0, 0);
136         gaw_rotate(cam_theta, 0, 1, 0);
137
138         draw_grid();
139
140         gaw_begin(GAW_QUADS);
141         gaw_color3f(1, 0, 0);
142         gaw_vertex2f(-1, -1);
143         gaw_color3f(0, 1, 0);
144         gaw_vertex2f(1, -1);
145         gaw_color3f(0, 0, 1);
146         gaw_vertex2f(1, 1);
147         gaw_color3f(1, 1, 0);
148         gaw_vertex2f(-1, 1);
149         gaw_end();
150
151         gaw_viewport(0, 0, win_width, win_height);
152 }
153
154 static void draw_grid(void)
155 {
156         gaw_begin(GAW_LINES);
157         gaw_color3f(0.5, 0, 0);
158         gaw_vertex4f(0, 0, 0, 1);
159         gaw_vertex4f(-1, 0, 0, 0);
160         gaw_vertex4f(0, 0, 0, 1);
161         gaw_vertex4f(1, 0, 0, 0);
162         gaw_color3f(0, 0.5, 0);
163         gaw_vertex4f(0, 0, 0, 1);
164         gaw_vertex4f(0, 0, -1, 0);
165         gaw_vertex4f(0, 0, 0, 1);
166         gaw_vertex4f(0, 0, 1, 0);
167         gaw_end();
168 }
169
170 static void mdl_reshape(int x, int y)
171 {
172         float aspect = (float)x / (float)(y - TOOLBAR_HEIGHT);
173
174         gaw_matrix_mode(GAW_PROJECTION);
175         gaw_load_identity();
176         gaw_perspective(50, aspect, 0.5, 100.0);
177
178         rtk_resize(toolbar, win_width, TOOLBAR_HEIGHT);
179 }
180
181 static void mdl_keyb(int key, int press)
182 {
183 }
184
185 static void mdl_mouse(int bn, int press, int x, int y)
186 {
187 }
188
189 static void mdl_motion(int x, int y)
190 {
191         int dx = x - mouse_x;
192         int dy = y - mouse_y;
193
194         if((dx | dy) == 0) return;
195
196         if(mouse_state[0]) {
197                 cam_theta += dx * 0.5f;
198                 cam_phi += dy * 0.5f;
199                 if(cam_phi < -90) cam_phi = -90;
200                 if(cam_phi > 90) cam_phi = 90;
201                 app_redisplay();
202         }
203
204         if(mouse_state[2]) {
205                 cam_dist += dy * 0.1f;
206                 if(cam_dist < 0) cam_dist = 0;
207                 app_redisplay();
208         }
209 }