[708] | 1 | #include "game.h" |
---|
| 2 | #include "disp.h" |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include <malloc.h> |
---|
| 5 | #include <user_lock.h> |
---|
| 6 | |
---|
| 7 | /////////////////////// |
---|
| 8 | // Global variables |
---|
| 9 | /////////////////////// |
---|
| 10 | |
---|
| 11 | unsigned char* buf[2]; // one image per buffer |
---|
| 12 | void * sts[2]; // for fbf_cma |
---|
| 13 | unsigned int cur_buf; // current buffer |
---|
| 14 | volatile unsigned int slice_x; // slice index (shared) |
---|
| 15 | volatile unsigned int slice_count; // slice count (shared) |
---|
| 16 | sqt_lock_t slice_get_lock; // slice index lock |
---|
| 17 | sqt_lock_t slice_done_lock; // slice index lock |
---|
| 18 | pthread_t trdid[1024]; // thread identifiers array |
---|
| 19 | Game game; // Game state |
---|
| 20 | |
---|
| 21 | // Textures |
---|
| 22 | unsigned char* g_tex[] = |
---|
| 23 | { |
---|
| 24 | NULL, // 0 |
---|
| 25 | NULL, // rock |
---|
| 26 | NULL, // door |
---|
| 27 | NULL, // handle |
---|
| 28 | NULL, // wood |
---|
| 29 | }; |
---|
| 30 | |
---|
| 31 | ////////////////////////// |
---|
| 32 | // Exported functions |
---|
| 33 | ////////////////////////// |
---|
| 34 | |
---|
| 35 | ////////////////////////////////////////// |
---|
| 36 | __attribute__((constructor)) void render() |
---|
| 37 | { |
---|
| 38 | unsigned int slice; |
---|
| 39 | |
---|
| 40 | while ( game.exit == 0 ) |
---|
| 41 | { |
---|
| 42 | dispRenderSlice( &slice ); |
---|
| 43 | } |
---|
| 44 | giet_pthread_exit( NULL ); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | //////////////////////////////////////// |
---|
| 48 | __attribute__((constructor)) void main() |
---|
| 49 | { |
---|
| 50 | unsigned int w, h, p; // platform parameters |
---|
| 51 | |
---|
| 52 | unsigned int i, j, n; // indexes for loops |
---|
| 53 | |
---|
| 54 | giet_tty_alloc(0); |
---|
| 55 | |
---|
| 56 | giet_tty_printf("\n[RAYCAST] enters main at cycle %d\n", |
---|
| 57 | giet_proctime() ); |
---|
| 58 | |
---|
| 59 | // get and check platform parameters |
---|
| 60 | giet_procs_number(&w, &h, &p); |
---|
| 61 | |
---|
| 62 | giet_pthread_assert( (w<=16) , "[RAYCAST ERROR] check hardware config" ); |
---|
| 63 | giet_pthread_assert( (h<=16) , "[RAYCAST ERROR] check hardware config" ); |
---|
| 64 | giet_pthread_assert( (p<= 4) , "[RAYCAST ERROR] check hardware config" ); |
---|
| 65 | |
---|
| 66 | // compute total number of threads |
---|
| 67 | unsigned int nthreads = w * h * p; |
---|
| 68 | |
---|
| 69 | // Initialize heap for each cluster |
---|
| 70 | for (i = 0; i < w; i++) |
---|
| 71 | for (j = 0; j < h; j++) |
---|
| 72 | heap_init(i, j); |
---|
| 73 | |
---|
| 74 | // Initialize lock protecting slice allocator |
---|
| 75 | sqt_lock_init( &slice_get_lock, w , h , p ); |
---|
| 76 | sqt_lock_init( &slice_done_lock, w , h , p ); |
---|
| 77 | |
---|
| 78 | // Allocate buffers |
---|
| 79 | buf[0] = malloc(FBUF_X_SIZE * FBUF_Y_SIZE); |
---|
| 80 | buf[1] = malloc(FBUF_X_SIZE * FBUF_Y_SIZE); |
---|
| 81 | sts[0] = malloc(64); |
---|
| 82 | sts[1] = malloc(64); |
---|
| 83 | |
---|
| 84 | // Initialize frame buffer and start Chained buffer DMA |
---|
| 85 | giet_fbf_cma_alloc(); |
---|
| 86 | giet_fbf_cma_init_buf(buf[0], buf[1], sts[0], sts[1]); |
---|
| 87 | giet_fbf_cma_start(FBUF_X_SIZE * FBUF_Y_SIZE); |
---|
| 88 | cur_buf = 0; |
---|
| 89 | |
---|
| 90 | // Load textures |
---|
| 91 | g_tex[1] = dispLoadTexture("misc/rock_32.raw"); |
---|
| 92 | g_tex[2] = dispLoadTexture("misc/door_32.raw"); |
---|
| 93 | g_tex[3] = dispLoadTexture("misc/handle_32.raw"); |
---|
| 94 | g_tex[4] = dispLoadTexture("misc/wood_32.raw"); |
---|
| 95 | |
---|
| 96 | // Initialise game |
---|
| 97 | gameInit(); |
---|
| 98 | |
---|
| 99 | giet_tty_printf("\n[RAYCAST] initialisation completed at cycle %d\n", |
---|
| 100 | giet_proctime() ); |
---|
| 101 | |
---|
| 102 | // launch other threads to speed render |
---|
| 103 | for ( n = 1 ; n < nthreads ; n++ ) |
---|
| 104 | { |
---|
| 105 | if ( giet_pthread_create( &trdid[n], |
---|
| 106 | NULL, // no attribute |
---|
| 107 | &render, |
---|
| 108 | NULL ) ) // no argument |
---|
| 109 | { |
---|
| 110 | giet_tty_printf("\n[RAYCAST ERROR] creating thread %d\n", n ); |
---|
| 111 | giet_pthread_exit( NULL ); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | // Game main loop : display one frame |
---|
| 118 | // and get one player move per iteration |
---|
| 119 | while ( game.exit == 0 ) |
---|
| 120 | { |
---|
| 121 | // initialise synchronisation variables |
---|
| 122 | // this actually allows the render threads to make useful work |
---|
| 123 | slice_count = 0; |
---|
| 124 | slice_x = 0; |
---|
| 125 | |
---|
| 126 | // contribute to build current buffer |
---|
| 127 | unsigned int slice; |
---|
| 128 | while ( dispRenderSlice( &slice ) ); |
---|
| 129 | /* |
---|
| 130 | unsigned int again; |
---|
| 131 | do |
---|
| 132 | { |
---|
| 133 | again = dispRenderSlice( &slice ); |
---|
| 134 | } |
---|
| 135 | while ( again ); |
---|
| 136 | */ |
---|
| 137 | // Wait last slice completion |
---|
| 138 | while (slice_count < FBUF_X_SIZE) giet_tty_printf(" "); |
---|
| 139 | |
---|
| 140 | // Flip buffer |
---|
| 141 | giet_fbf_cma_display(cur_buf); |
---|
| 142 | cur_buf = 1 - cur_buf; |
---|
| 143 | |
---|
| 144 | // get new player position [x,y,dir] |
---|
| 145 | gameControl(); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | giet_pthread_exit( NULL ); |
---|
| 149 | |
---|
| 150 | } // end main() |
---|