Ignore:
Timestamp:
Oct 1, 2015, 4:09:25 PM (9 years ago)
Author:
alain
Message:

Adapt the following application to the POSIX threads API

  • convol
  • classif
  • raycast
  • coproc
  • display
  • gameoflife
  • transpose
  • shell
File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/applications/raycast/disp.c

    r692 r708  
    66#include <math.h>
    77#include <hard_config.h>
    8 #include <user_sqt_lock.h>
     8#include <user_lock.h>
    99
    1010#define FIELD_OF_VIEW   (70.f * M_PI / 180.f)   // Camera field of view
     
    1313#define FLOOR_COLOR     (0x33)                  // dark gray
    1414
    15 // Globals
    16 
    17 static unsigned char*           buf[2];         // framebuffer
    18 static void *                   sts[2];         // for fbf_cma
    19 static unsigned int             cur_buf;        // current framebuffer
    20 static volatile unsigned int    slice_x;        // slice index (shared)
    21 static sqt_lock_t               slice_x_lock;   // slice index lock
    22 static volatile unsigned int    slice_cnt;      // slice count (shared)
    23 
    24 // Textures indexed by block number
    25 static unsigned char *g_tex[] =
    26 {
    27     NULL, // 0
    28     NULL, // rock
    29     NULL, // door
    30     NULL, // handle
    31     NULL, // wood
    32 };
    33 
     15
     16////////////////////////////
     17// Extern variables
     18////////////////////////////
     19
     20extern unsigned char*  g_tex[5];
     21extern unsigned char*  buf[2];
     22extern void*           sts[2];
     23extern unsigned int    cur_buf;
     24extern unsigned int    slice_x;
     25extern unsigned int    slice_count;
     26extern sqt_lock_t      slice_get_lock;
     27extern sqt_lock_t      slice_done_lock;
     28extern Game            game;
     29
     30//////////////////////
    3431// Local functions
    35 
     32////////////////////////
     33
     34/////////////////////////////////////////////////////////////////////////
    3635static void dispDrawColumnTex(int x, int y0, int y1, unsigned char *line)
    3736{
     
    4746}
    4847
     48///////////////////////////////////////////////////////////////////////////
    4949static void dispDrawColumnSolid(int x, int y0, int y1, unsigned char color)
    5050{
     
    5757}
    5858
    59 static void dispDrawSlice(Game *game, int x, int height, int type, int tx)
     59////////////////////////////////////////////////////////////////
     60static void dispDrawSlice( int x, int height, int type, int tx )
    6061{
    6162    // Ceiling
     
    9091}
    9192
    92 static float dispRaycast(Game *game, int *type, float *tx, float angle)
    93 {
    94     float x = game->player.x;
    95     float y = game->player.y;
     93///////////////////////////////////////////////////////////////////////
     94static float dispRaycast( int *type, float *tx, float angle )
     95{
     96    float x = game.player.x;
     97    float y = game.player.y;
    9698
    9799    // Camera is inside a block.
     
    162164}
    163165
     166////////////////////////////////////////////////////////////////
    164167static void dispTranspose(unsigned char *buf, unsigned int size)
    165168{
     
    179182}
    180183
    181 static unsigned char *dispLoadTexture(char *path)
     184////////////////////////
     185// Exported functions
     186////////////////////////
     187
     188//////////////////////////////////////////
     189unsigned char* dispLoadTexture(char *path)
    182190{
    183191    int fd;
     
    186194    tex = malloc(TEX_SIZE * TEX_SIZE);
    187195    fd = giet_fat_open(path, O_RDONLY);
    188     if (fd < 0) {
     196    if (fd < 0)
     197    {
    189198        free(tex);
    190199        return NULL;
     
    196205    dispTranspose(tex, TEX_SIZE);
    197206
    198     giet_tty_printf("[RAYCAST] loaded tex %s\n", path);
    199 
    200207    return tex;
    201208}
    202209
    203 // Exported functions
    204 
    205 void dispInit()
    206 {
    207     unsigned int w, h, p;
    208 
    209     // Initialize lock
    210     giet_procs_number(&w, &h, &p);
    211     sqt_lock_init(&slice_x_lock, w, h, p);
    212 
    213     // Allocate framebuffer
    214     buf[0] = malloc(FBUF_X_SIZE * FBUF_Y_SIZE);
    215     buf[1] = malloc(FBUF_X_SIZE * FBUF_Y_SIZE);
    216     sts[0] = malloc(64);
    217     sts[1] = malloc(64);
    218 
    219     // Initialize framebuffer
    220     giet_fbf_cma_alloc();
    221     giet_fbf_cma_init_buf(buf[0], buf[1], sts[0], sts[1]);
    222     giet_fbf_cma_start(FBUF_X_SIZE * FBUF_Y_SIZE);
    223 
    224     // Load textures
    225     g_tex[1] = dispLoadTexture("misc/rock_32.raw");
    226     g_tex[2] = dispLoadTexture("misc/door_32.raw");
    227     g_tex[3] = dispLoadTexture("misc/handle_32.raw");
    228     g_tex[4] = dispLoadTexture("misc/wood_32.raw");
    229 
    230     cur_buf = 0;
    231     slice_cnt = 0;
    232     slice_x = FBUF_X_SIZE;
    233 }
    234 
    235 int dispRenderSlice(Game *game)
    236 {
    237     unsigned int x;
     210///////////////////////////////////////////////////
     211unsigned int dispRenderSlice( unsigned int* slice )
     212{
     213    // return 0 when there is no more slice to do
     214
    238215    int type;
    239216    float angle, dist, tx;
    240 
    241     sqt_lock_acquire(&slice_x_lock);
    242 
    243     if (slice_x == FBUF_X_SIZE) {
    244         // No more work to do for this frame
    245         sqt_lock_release(&slice_x_lock);
     217    unsigned int x;
     218
     219    // get a slice index
     220    sqt_lock_acquire( &slice_get_lock );
     221
     222    if (slice_x >= FBUF_X_SIZE)      // No more work to do for this frame
     223    {
     224        sqt_lock_release( &slice_get_lock );
    246225        return 0;
    247226    }
    248     else {
    249         // Keep slice coordinate
    250         x = slice_x++;
    251     }
    252 
    253     sqt_lock_release(&slice_x_lock);
    254 
    255     angle = game->player.dir - FIELD_OF_VIEW / 2.f +
     227    else                             // Keep slice coordinate
     228    {
     229        x       = slice_x;
     230        slice_x = x + 1;
     231        sqt_lock_release( &slice_get_lock );
     232        *slice  = x;
     233    }
     234
     235    // Cast a ray to get wall distance
     236    angle = game.player.dir - FIELD_OF_VIEW / 2.f +
    256237            x * FIELD_OF_VIEW / FBUF_X_SIZE;
    257238
    258     // Cast a ray to get wall distance
    259     dist = dispRaycast(game, &type, &tx, angle);
    260 
    261     // Perspective correction
    262     dist *= cos(game->player.dir - angle);
     239    dist = dispRaycast(&type, &tx, angle);
     240
     241    dist *= cos(game.player.dir - angle);
    263242
    264243    // Draw ceiling, wall and floor
    265     dispDrawSlice(game, x, FBUF_Y_SIZE / dist, type, tx * TEX_SIZE);
     244    dispDrawSlice(x, FBUF_Y_SIZE / dist, type, tx * TEX_SIZE);
    266245
    267246    // Signal this slice is done
    268     atomic_increment((unsigned int*)&slice_cnt, 1);
     247
     248    sqt_lock_acquire( &slice_done_lock );
     249    slice_count++;
     250    sqt_lock_release( &slice_done_lock );
    269251
    270252    return 1;
    271 }
    272 
    273 void dispRender(Game *game)
    274 {
    275     int start = giet_proctime();
    276 
    277     // Start rendering
    278     slice_cnt = 0;
    279     slice_x = 0;
    280 
    281     // Render slices
    282     while (dispRenderSlice(game));
    283 
    284     // Wait for completion
    285     while (slice_cnt != FBUF_X_SIZE);
    286 
    287     // Flip framebuffer
    288     giet_fbf_cma_display(cur_buf);
    289     cur_buf = !cur_buf;
    290     giet_tty_printf("[RAYCAST] flip (took %d cycles)\n", giet_proctime() - start);
    291 }
    292 
     253}  // end dispRenderSlice()
     254
     255
Note: See TracChangeset for help on using the changeset viewer.