[Xmltools-development] xmltools2/xmltools/tests cars.c cars.h graphics.c graphics.h main.c racecore.
Status: Beta
Brought to you by:
nik_89
|
From: nik_89 <ni...@us...> - 2004-11-29 06:26:08
|
Update of /cvsroot/xmltools/xmltools2/xmltools/tests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16460 Modified Files: cars.c cars.h graphics.c graphics.h main.c racecore.c Log Message: Added support for 3 new graphics : the starting tile, the lap numbers and the small leds on the starting track. the game now has an ending, the lap are shown correctly and the ending waits until both cars come to a complete stop. whats left is the starting animation with the leds and the winning image. this is for the laps part. Theres still other stuff to do, but they are polishing really. Index: graphics.c =================================================================== RCS file: /cvsroot/xmltools/xmltools2/xmltools/tests/graphics.c,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** graphics.c 14 Nov 2004 14:49:50 -0000 1.11 --- graphics.c 29 Nov 2004 06:25:57 -0000 1.12 *************** *** 8,11 **** --- 8,12 ---- #include "cars.h" + #include "track.h" /* TRACK_START_X and _Y */ #include "graphics.h" *************** *** 15,21 **** --- 16,27 ---- static SDL_Surface *flag = NULL; static SDL_Surface *cars = NULL; + static SDL_Surface *laps = NULL; /* lap numbers */ + static SDL_Surface *stile = NULL; /* the starting track */ + static SDL_Surface *leds = NULL; /* small lights on the starting track */ static void draw_pixel(int x, int y, int color); + static const int myleds[4] = { 29 - 3, 45 - 3, 61 - 3, 77 - 3}; + /* methods */ *************** *** 115,122 **** } ! void Graphics_DrawTile(int x, int y, int index) { SDL_Rect src, dest; if (screen && tiles) { --- 121,192 ---- } ! void Graphics_DrawLed(int led) { SDL_Rect src, dest; + if (screen && leds) + { + dest.x = TRACK_XOFFSET + TRACK_START_X * TILE_WIDTH ; + dest.y = TRACK_YOFFSET + TRACK_START_Y * TILE_HEIGHT; + + dest.y += LEDS_YOFFSET - 3; + dest.x += myleds[led]; + + src.x = 0; + src.y = 0; + src.w = 11; + src.h = 11; + + SDL_BlitSurface(leds, &src, screen, &dest); + } + } + + void Graphics_DrawStartTile(int x, int y) + { + SDL_Rect src, dest; + + if (screen && stile) + { + dest.x = x - TILE_EXTRAX; + dest.y = y - TILE_EXTRAY; + + src.x = 0; + src.y = 0; + src.w = TILE_EXTRAX + TILE_WIDTH + TILE_EXTRAX; + src.h = TILE_EXTRAY + TILE_HEIGHT + TILE_EXTRAY; + + SDL_BlitSurface(stile, &src, screen, &dest); + } + } + + + void Graphics_DrawLapNum(int player, int lap) + { + SDL_Rect src, dest; + + if (screen && laps) + { + int x, y; + dest.x = TRACK_XOFFSET + TRACK_START_X * TILE_WIDTH; + dest.y = TRACK_YOFFSET + TRACK_START_Y * TILE_HEIGHT + TRACK_YOFFSET - LAP_NUMOFFSET0; + if (player) + dest.y += TILE_HEIGHT - TRACK_YOFFSET - LAP_NUMOFFSET1; + + if (lap <= LAPS_MAX) + src.x = lap * LAPS_WIDTH; + src.y = 0; + src.w = LAPS_WIDTH; + src.h = LAPS_HEIGHT; + if (src.x >= 0 && src.x < tiles->w) + { + SDL_BlitSurface(laps, &src, screen, &dest); + } + } + } + + void Graphics_DrawTile(int x, int y, int index) + { + SDL_Rect src, dest; + if (screen && tiles) { *************** *** 160,164 **** /* draw centered. fx: (x, y) is the mid point of the car, in * this program most likely between the front wheels */ ! /* fixme: go into cars.c and fix the offsets so this works */ dest.x = x; // - src.w / 2; dest.y = y; // - src.h / 2; --- 230,234 ---- /* draw centered. fx: (x, y) is the mid point of the car, in * this program most likely between the front wheels */ ! /* FIXME: go into cars.c and fix the offsets so this works */ dest.x = x; // - src.w / 2; dest.y = y; // - src.h / 2; *************** *** 270,278 **** SDL_MapRGB(cars->format, 255, 0, 255)); } - SDL_WM_SetCaption("racing", NULL); ! if (!screen || !tiles || !background || !flag) failed = 1; --- 340,368 ---- SDL_MapRGB(cars->format, 255, 0, 255)); } + laps = SDL_LoadBMP("graphics/lapnumbers.bmp"); + if (laps) + { + SDL_SetColorKey(laps, SDL_SRCCOLORKEY, + SDL_MapRGB(laps->format, 255, 0, 255)); + } + + stile = SDL_LoadBMP("graphics/starttrack.bmp"); + if (stile) + { + SDL_SetColorKey(stile, SDL_SRCCOLORKEY, + SDL_MapRGB(laps->format, 255, 0, 255)); + } + + leds = SDL_LoadBMP("graphics/leds.bmp"); + if (leds) + { + SDL_SetColorKey(leds, SDL_SRCCOLORKEY, + SDL_MapRGB(laps->format, 255, 0, 255)); + } + SDL_WM_SetCaption("racing", NULL); ! if (!screen || !tiles || !background || !flag || !cars || !laps || !stile || !leds) failed = 1; Index: cars.h =================================================================== RCS file: /cvsroot/xmltools/xmltools2/xmltools/tests/cars.h,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** cars.h 14 Nov 2004 14:48:19 -0000 1.15 --- cars.h 29 Nov 2004 06:25:57 -0000 1.16 *************** *** 8,11 **** --- 8,13 ---- #define __CARS_H + /* for now only 2 players r supported + * we dont intend to support more than 2 */ #define NUM_PLAYERS 2 *************** *** 52,57 **** --- 54,66 ---- #define CARS_CTURNSTART_OFFSET 8 + /* def: 5 + * track laps + * don't add more than 5 laps since the + * lap number only has 6 numbers, 0 to 5*/ + #define CARS_LAP_TOTAL 5 + typedef struct { + int carid; int xpos, ypos; int xfix, yfix; /* to align the cars gfx */ Index: racecore.c =================================================================== RCS file: /cvsroot/xmltools/xmltools2/xmltools/tests/racecore.c,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** racecore.c 14 Nov 2004 16:42:17 -0000 1.10 --- racecore.c 29 Nov 2004 06:25:57 -0000 1.11 *************** *** 140,143 **** --- 140,148 ---- unsigned char tile = Track_GetTile(x, y); + if (x == TRACK_START_X && y == TRACK_START_Y) + Graphics_DrawStartTile( + TRACK_XOFFSET + x * TILE_WIDTH, + TRACK_YOFFSET + y * TILE_HEIGHT); + else if (tile != TT_NONE) { Index: graphics.h =================================================================== RCS file: /cvsroot/xmltools/xmltools2/xmltools/tests/graphics.h,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** graphics.h 14 Nov 2004 14:52:54 -0000 1.6 --- graphics.h 29 Nov 2004 06:25:57 -0000 1.7 *************** *** 25,28 **** --- 25,40 ---- #define CARS_TILES_HIGH 2 + #define LAPS_WIDTH 10 + #define LAPS_HEIGHT 11 + + #define LAP_NUMOFFSET0 17 + #define LAP_NUMOFFSET1 15 + + /* Laps numbers maximum in the bmp */ + #define LAPS_MAX 5 + + /* Leds related */ + #define LEDS_YOFFSET 27 + extern void Graphics_DebugCircle(); *************** *** 33,36 **** --- 45,51 ---- extern void Graphics_DrawBackground(void); extern void Graphics_DrawFlag(void); + extern void Graphics_DrawLapNum(int player, int lap); + extern void Graphics_DrawStartTile(int x, int y); + extern void Graphics_DrawLed(int led); extern void Graphics_ClearScreen(void); Index: main.c =================================================================== RCS file: /cvsroot/xmltools/xmltools2/xmltools/tests/main.c,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** main.c 24 Oct 2004 02:30:24 -0000 1.13 --- main.c 29 Nov 2004 06:25:57 -0000 1.14 *************** *** 63,71 **** failed = 1; } - else if (Cars_Init()) - { - fprintf(stderr, "cars init failed\n"); - failed = 1; - } else { --- 63,66 ---- Index: cars.c =================================================================== RCS file: /cvsroot/xmltools/xmltools2/xmltools/tests/cars.c,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** cars.c 14 Nov 2004 16:42:17 -0000 1.24 --- cars.c 29 Nov 2004 06:25:56 -0000 1.25 *************** *** 23,27 **** static void car_offtrack(CAR_DATA *car); ! /* forward declarations */ --- 23,28 ---- static void car_offtrack(CAR_DATA *car); ! static void finish_race(CAR_DATA *car); ! static void ending(); /* forward declarations */ *************** *** 30,40 **** static CAR_DATA mycars[NUM_PLAYERS]; /* local */ static void car_offtrack(CAR_DATA *car) { int i; - if (car->speed > 0 && car->offtrack == 1) { --- 31,77 ---- static CAR_DATA mycars[NUM_PLAYERS]; + /* winner car pointer */ + CAR_DATA *winner = NULL; + + /* 1 if all the players finished */ + unsigned char finished = 0; /* used mainly to finish + the ending brake of the last car + (when it passes the finish line) */ /* local */ + static void ending() + { + /* race finished we need to show the winning image + * and go into the editor state after */ + + if (winner) + printf("Player %d wins the race!!\n", winner->carid); + + state = STATE_EDITOR; + } + + static void finish_race(CAR_DATA *car) + { + unsigned int a = 0, racing = 0; + + car->racing = 0; + /* check out all the players to see if they finished */ + while (a < NUM_PLAYERS) + { + if (mycars[a].racing == 1) + { + racing++; + break; + } + a++; + } + if (racing == 1) + winner = car; /* the current player was the first to finish */ + } + static void car_offtrack(CAR_DATA *car) { int i; if (car->speed > 0 && car->offtrack == 1) { *************** *** 492,499 **** { memset(car, 0, sizeof(CAR_DATA)); ! car->xfix = CARS_GFX_FIX_X * 0; car->yfix = CARS_GFX_FIX_Y * -1; car->current.x = TRACK_START_X; car->current.y = TRACK_START_Y; --- 529,541 ---- { memset(car, 0, sizeof(CAR_DATA)); ! ! car->carid = lane; /* unique number based on the starting lane */ car->xfix = CARS_GFX_FIX_X * 0; car->yfix = CARS_GFX_FIX_Y * -1; + + car->lap = CARS_LAP_TOTAL; + Graphics_DrawLapNum(car->carid, car->lap); + car->racing = 1; car->current.x = TRACK_START_X; car->current.y = TRACK_START_Y; *************** *** 539,542 **** --- 581,587 ---- void Cars_Move(CAR_DATA *car) { + Graphics_DrawLapNum(car->carid, car->lap); + Graphics_DrawLed(car->lap % 4); /* just to test it */ + if (car->offtrack) { *************** *** 545,555 **** else { int tx = car->current.x; int ty = car->current.y; if (car->plunger && car->speed < 50) ! car->speed = car->speed + 1; else if (!car->plunger && car->speed > 0) ! car->speed = car->speed - CARS_BRAKE_OFFSET; cars_movetrack(car); --- 590,615 ---- else { + if (car->speed <= 0 && !car->racing) + { + if (finished == 3) + ending(); + else + finished |= car->carid + 1; + + return; + } int tx = car->current.x; int ty = car->current.y; + + if (!car->racing) + { + car->plunger = 0; + car->speed -= CARS_BRAKE_OFFSET + 1; + } if (car->plunger && car->speed < 50) ! car->speed += 1; else if (!car->plunger && car->speed > 0) ! car->speed -= CARS_BRAKE_OFFSET; cars_movetrack(car); *************** *** 576,579 **** --- 636,650 ---- car->current.type, car->direction); #endif + if (car->current.x == TRACK_START_X && car->current.y == TRACK_START_Y) + { + if (car->lap <= 0) + { + finish_race(car); + return; + } + car->lap--; + Graphics_DrawLapNum(car->carid, car->lap); + } + } } |