added 'videolib' snapshot
[k8-jellyphysics.git] / src / videolib / videolib.h
blob39c5c7082e9731d6f959f9ad1da9989f6add43ff
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
9 */
10 #ifndef VIDEOLIB_H
11 #define VIDEOLIB_H
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
17 #include <SDL.h>
20 ////////////////////////////////////////////////////////////////////////////////
21 //#define VIDEOLIB_ENABLE_DEBUG_LOG
22 //#define VIDEOLIB_ENABLE_SFONT
23 //#define VIDEOLIB_ENABLE_READ_STR
24 //#define VIDEOLIB_ENABLE_CIRCLE_ELLIPSE
25 //#define VIDEOLIB_ENABLE_TEXT
26 //#define VIDEOLIB_ENABLE_PRTEXT
27 //#define VIDEOLIB_ENABLE_CLOCK
28 //#define VIDEOLIB_ENABLE_POLYMOD
29 //#define VIDEOLIB_ENABLE_BGICHR
30 //#define VIDEOLIB_ENABLE_MAIN_LOOP
32 /* we need clock module for main loop */
33 #ifdef VIDEOLIB_ENABLE_MAIN_LOOP
34 # ifndef VIDEOLIB_ENABLE_CLOCK
35 # define VIDEOLIB_ENABLE_CLOCK
36 # endif
37 #endif
39 /* we need draw_str() for vl_read_str() */
40 #ifdef VIDEOLIB_ENABLE_READ_STR
41 # ifndef VIDEOLIB_ENABLE_CLOCK
42 # define VIDEOLIB_ENABLE_CLOCK
43 # endif
44 #endif
46 ////////////////////////////////////////////////////////////////////////////////
47 #include "videolib_dbglog.h"
50 ////////////////////////////////////////////////////////////////////////////////
51 #define TRANSPARENT_COLOR (0xff000000u)
53 #define READSTR_QUIT ((const char *)1)
55 #ifndef SCR_WIDTH
56 # define SCR_WIDTH (320)
57 #endif
58 #ifndef SCR_HEIGHT
59 # define SCR_HEIGHT (240)
60 #endif
63 ////////////////////////////////////////////////////////////////////////////////
64 typedef enum {
65 SDL_FILTER_NONE,
66 SDL_FILTER_BLACKNWHITE,
67 SDL_FILTER_GREEN
68 } sdl_filter_t;
71 ////////////////////////////////////////////////////////////////////////////////
72 extern int sdl_fs; // this can be set to 1 before calling videolib_init()
73 extern int sdl_opengl; // this can be set to 0 before calling videolib_init()
74 extern int sdl_vsync; // !0: VBLWAIT; default: 1
75 extern int sdl_real_fs; // use 'real' fullscreen? defaul: 0
77 extern int sdl_mag2x; // set to !0 to create double-sized window; default: 1; have no effect after calling videolib_init()
78 extern int sdl_scanlines; // set to !0 to use 'scanline' filter in mag2x mode; default: 1
79 extern sdl_filter_t sdl_filter; // default: SDL_FILTER_NONE
81 extern SDL_Window *sdl_win;
82 extern SDL_Renderer *sdl_rend;
83 extern SDL_Texture *sdl_scr;
84 extern Uint32 *sdl_vscr;
87 ////////////////////////////////////////////////////////////////////////////////
88 extern void videolib_args (int *argc, char *argv[]);
90 extern int videolib_init (const char *window_name);
91 extern int videolib_deinit (void); /* you don't need to call this */
92 extern void switch_fullscreen (void);
93 extern void post_quit_message (void);
95 ////////////////////////////////////////////////////////////////////////////////
96 static inline Uint32 rgb2col (Uint8 r, Uint8 g, Uint8 b) { return (r<<16)|(g<<8)|b; }
97 static inline Uint32 rgba2col (Uint8 r, Uint8 g, Uint8 b, Uint8 a) { return (a<<24)|(r<<16)|(g<<8)|b; }
99 static inline void put_pixel (int x, int y, Uint32 col) {
100 if (x >= 0 && y >= 0 && x < SCR_WIDTH && y < SCR_HEIGHT && (col&0xff000000u) != 0xff000000u) {
101 Uint32 *da = &sdl_vscr[y*SCR_WIDTH+x];
102 if (col&0xff000000u) {
103 const Uint32 a = 256-(col>>24); /* to not loose bits */
104 const Uint32 s = col&0xffffff;
105 const Uint32 dc = (*da)>>8;
106 const Uint32 srb = (s&0xff00ff);
107 const Uint32 sg = (s&0x00ff00);
108 const Uint32 drb = (dc&0xff00ff);
109 const Uint32 dg = (dc&0x00ff00);
110 const Uint32 orb = (drb+(((srb-drb)*a+0x800080)>>8))&0xff00ff;
111 const Uint32 og = (dg+(((sg-dg)*a+0x008000)>>8))&0x00ff00;
112 *da = orb|og;
113 } else {
114 *da = col;
119 ////////////////////////////////////////////////////////////////////////////////
120 extern int sdl_frame_changed; // this will be set to 0 by paint_screen()
122 extern void paint_screen (void);
124 ////////////////////////////////////////////////////////////////////////////////
125 #ifdef VIDEOLIB_ENABLE_TEXT
126 extern void draw_char (int x, int y, char ch, Uint32 col, Uint32 bkcol);
127 extern void draw_str (int x, int y, const char *str, Uint32 col, Uint32 bkcol);
128 #endif
130 #ifdef VIDEOLIB_ENABLE_PRTEXT
131 extern int char_width_pr (char ch);
132 extern int str_width_pr (const char *str);
133 extern int draw_char_pr (int x, int y, char ch, Uint32 col, Uint32 bkcol);
134 extern int draw_str_pr (int x, int y, const char *str, Uint32 col, Uint32 bkcol);
135 #endif
137 ////////////////////////////////////////////////////////////////////////////////
138 #ifdef VIDEOLIB_ENABLE_SFONT
139 extern int char_width_sf (char ch);
140 extern int str_width_sf (const char *str);
141 extern int draw_char_sf (char ch, int x, int y, Uint32 c0, Uint32 c1);
142 extern int draw_str_sf (const char *str, int x, int y, Uint32 c0, Uint32 c1);
143 #endif
145 ////////////////////////////////////////////////////////////////////////////////
146 #ifdef VIDEOLIB_ENABLE_READ_STR
147 extern const char *vl_read_str (void);
148 #endif
150 ////////////////////////////////////////////////////////////////////////////////
151 extern void draw_hline (int x0, int y0, int len, Uint32 col);
152 extern void draw_vline (int x0, int y0, int len, Uint32 col);
153 extern void draw_line (int x0, int y0, int x1, int y1, Uint32 col);
154 /* draw line without last point */
155 extern void draw_line_no_last (int x0, int y0, int x1, int y1, Uint32 col);
157 extern void fill_rect (int x, int y, int w, int h, Uint32 col);
158 extern void draw_rect (int x, int y, int w, int h, Uint32 col);
159 /* 4 phases */
160 extern void draw_selection_rect (int phase, int x0, int y0, int wdt, int hgt, Uint32 col0, Uint32 col1);
162 #ifdef VIDEOLIB_ENABLE_CIRCLE_ELLIPSE
163 extern void draw_circle (int cx, int cy, int radius, Uint32 clr);
164 extern void draw_ellipse (int x0, int y0, int x1, int y1, Uint32 clr);
165 extern void draw_filled_circle (int cx, int cy, int radius, Uint32 clr);
166 extern void draw_filled_ellipse (int x0, int y0, int x1, int y1, Uint32 clr);
167 #endif
169 #ifdef VIDEOLIB_ENABLE_CLOCK
170 ////////////////////////////////////////////////////////////////////////////////
171 /* returns monitonically increasing time; starting value is UNDEFINED (i.e. can be any number) */
172 extern Uint64 vl_clock_milli (void); // milliseconds; (0: no timer available)
173 extern Uint64 vl_clock_micro (void); // microseconds; (0: no timer available)
174 #endif
176 ////////////////////////////////////////////////////////////////////////////////
177 #ifdef VIDEOLIB_ENABLE_POLYMOD
178 # include "videolib_polymod.h"
179 extern void vl_polymod_fill_scr (Uint32 col); /* called after vl_polymod_end() to fill resulting poly with draw_line() */
180 #endif
182 ////////////////////////////////////////////////////////////////////////////////
183 #include "videolib_bgichr.h"
185 ////////////////////////////////////////////////////////////////////////////////
186 // main loop
188 enum {
189 VL_EVT_SHOW_FRAME = 1,
190 VL_EVT_USER = 128
194 #ifdef VIDEOLIB_ENABLE_MAIN_LOOP
195 ////////////////////////////////////////////////////////////////////////////////
196 /* elapsed_ms: ms elapsed from the last call; will try to keep up with FPS; 0: first call in vl_main_loop() */
197 /* can set sdl_frame_changed flag so vl_main_loop() will call vl_rebuild() or call vl_rebuild() itself */
198 extern void (*vl_update) (int elapsed_ms);
200 /* reset sdl_frame_changed flag in vl_rebuild() to avoid excessive calls */
201 extern void (*vl_rebuild) (void);
203 /* return >0 if event was eaten by handler; <0 to exit loop and 0 to go on */
204 extern int (*vl_preprocess_event) (SDL_Event *ev);
206 extern void (*vl_postprocess_event) (SDL_Event *ev);
208 extern void (*vl_process_keydown) (SDL_KeyboardEvent *ev);
209 extern void (*vl_process_keyup) (SDL_KeyboardEvent *ev);
211 extern void (*vl_process_mousedown) (SDL_MouseButtonEvent *ev);
212 extern void (*vl_process_mouseup) (SDL_MouseButtonEvent *ev);
213 extern void (*vl_process_mousemotion) (SDL_MouseMotionEvent *ev);
214 extern void (*vl_process_mousewheel) (SDL_MouseWheelEvent *ev);
217 extern int vl_fps; // desired FPS (default: 60)
220 /* returns !0 to stop event loop; <0: from event preprocessor; >0: from SDL_QUIT */
221 extern int vl_process_event (SDL_Event *ev);
223 extern void vl_start_fps_timer (void);
224 extern void vl_stop_fps_timer (void);
226 /* will start and stop FPS timer */
227 extern void vl_main_loop (void);
228 #endif
231 #ifdef __cplusplus
233 #endif
234 #endif