test/snake/snake.c

changeset 89
e1f682a8a145
parent 88
6234b7ea48f3
equal deleted inserted replaced
88:6234b7ea48f3 89:e1f682a8a145
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE. 25 * POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28 #include "ascension/core.h" 28 #include <ascension/core.h>
29 #include <ascension/ui.h> 29 #include <ascension/ui.h>
30 30
31 #include <cx/printf.h> 31 #include <cx/printf.h>
32
33 enum Textures2d {
34 TEX_SHIP = 0,
35 TEX2D_COUNT
36 };
37 static AscTexture tex2d[TEX2D_COUNT];
38 #define TEXTURE_SHIP &tex2d[TEX_SHIP]
39
40 static void init_textures(void) {
41 asc_texture_init_2d(tex2d, TEX2D_COUNT);
42 asc_texture_from_file(TEXTURE_SHIP, "ship.png");
43 }
44
45 static void destroy_textures(void) {
46 asc_texture_destroy(tex2d, TEX2D_COUNT);
47 }
32 48
33 static void update_fps_counter(AscSceneNode *node) { 49 static void update_fps_counter(AscSceneNode *node) {
34 static uint64_t last_fps = 0; 50 static uint64_t last_fps = 0;
35 static uint64_t debounce = ASC_NANOS_SECOND - 1; 51 static uint64_t debounce = ASC_NANOS_SECOND - 1;
36 debounce += asc_context.frame_nanos; 52 debounce += asc_context.frame_nanos;
73 .text = "Score: 0" 89 .text = "Score: 0"
74 ); 90 );
75 asc_add_ui_node(node); 91 asc_add_ui_node(node);
76 } 92 }
77 93
78 static void free_spaceship(AscSceneNode *node) {
79 asc_texture_destroy(&((AscSprite*)node)->tex);
80 free(node);
81 }
82
83 static void create_spaceship(void) { 94 static void create_spaceship(void) {
84 // TODO: textures should be passed by pointers (and probably ref-counted)
85 AscTexture texture;
86 asc_texture_init_2d(&texture);
87 asc_texture_from_file(&texture, "ship.png");
88 AscSceneNode *sprite = asc_sprite( 95 AscSceneNode *sprite = asc_sprite(
89 .texture = texture, 96 .texture = TEXTURE_SHIP,
90 .x = 250, 97 .x = 250,
91 .y = 300, 98 .y = 300,
92 .width = 64, 99 .width = 64,
93 .height = 64 100 .height = 64
94 ); 101 );
95 102
96 // TODO: add to 2D scene instead of UI scene, once we have one 103 // TODO: add to 2D scene instead of UI scene, once we have one
97 asc_add_ui_node(sprite); 104 asc_add_ui_node(sprite);
98 105
99 // TODO: remove this hack by refactoring how textures work
100 sprite->free_func = free_spaceship;
101
102 // TODO: return something 106 // TODO: return something
103 } 107 }
104 108
105 int main(int argc, char** argv) { 109 int main(int argc, char** argv) {
106 asc_context_initialize(); 110 asc_context_initialize();
107 if (asc_has_error()) { 111 if (asc_has_error()) {
108 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 112 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
109 "Fatal Error",asc_get_error(),NULL); 113 "Fatal Error",asc_get_error(), NULL);
110 return 1; 114 return 1;
111 } 115 }
112 #ifdef TEST_BUILD 116 #ifdef TEST_BUILD
113 asc_set_font_path("../../fonts"); 117 asc_set_font_path("../../fonts");
114 asc_set_shader_path("../../shader"); 118 asc_set_shader_path("../../shader");
120 // create window 124 // create window
121 AscWindowSettings settings; 125 AscWindowSettings settings;
122 asc_window_settings_init_defaults(&settings); 126 asc_window_settings_init_defaults(&settings);
123 settings.title = "Snake"; 127 settings.title = "Snake";
124 asc_window_initialize(0, &settings); 128 asc_window_initialize(0, &settings);
129
130 // load textures
131 init_textures();
125 132
126 // create UI elements 133 // create UI elements
127 create_fps_counter(); 134 create_fps_counter();
128 create_score_counter(); 135 create_score_counter();
129 136
145 if (asc_key_pressed(ESCAPE)) { 152 if (asc_key_pressed(ESCAPE)) {
146 asc_context_quit(); 153 asc_context_quit();
147 } 154 }
148 } while (asc_loop_next()); 155 } while (asc_loop_next());
149 156
157 // cleanup
158 destroy_textures();
150 asc_context_destroy(); 159 asc_context_destroy();
151 return 0; 160 return 0;
152 } 161 }
153 162

mercurial