test/nds-test-progs/general/source/main.c
author Darren Alton <dalton@stevens.edu>
Mon, 18 Aug 2008 07:29:52 +0000
branchgsoc2008_nds
changeset 2696 0b395a60deff
parent 2694 c1c7cb1b5a47
child 2698 e1da92da346c
permissions -rwxr-xr-x
Timers work now.
     1 
     2 #include <SDL/SDL.h>
     3 #if defined(NDS) || defined(__NDS__) || defined (__NDS)
     4 #include <nds.h>
     5 #include <fat.h>
     6 #else
     7 #define swiWaitForVBlank() 
     8 #define consoleDemoInit() 
     9 #define fatInitDefault() 
    10 #define RGB15(r,g,b) SDL_MapRGB(screen->format,((r)<<3),((g)<<3),((b)<<3))
    11 #endif
    12 
    13 void splash(SDL_Surface *screen, int s) {
    14 	SDL_Surface *logo;
    15 	SDL_Rect area = {0,0,256,192};
    16 
    17 	logo = SDL_LoadBMP("sdl.bmp");
    18 	if(!logo) {
    19 		printf("Couldn't splash.\n");
    20 		return;
    21 	}
    22 	/*logo->flags &= ~SDL_PREALLOC;*/
    23 	SDL_BlitSurface(logo, NULL, screen, &area);
    24 	SDL_Flip(screen);
    25 	while(s-- > 0) {
    26 		int i = 60;
    27 		while(--i) swiWaitForVBlank();
    28 	}
    29 }
    30 
    31 int main(void) {
    32 	SDL_Surface *screen;
    33 	SDL_Joystick *stick;
    34 	SDL_Event event;
    35 	SDL_Rect rect = {0,0,256,192};
    36 	int i;
    37 
    38 	consoleDemoInit(); puts("Hello world!  Initializing FAT...");
    39 	fatInitDefault();
    40 	if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0) {
    41 		puts("# error initializing SDL");
    42 		puts(SDL_GetError());
    43 		return 1;
    44 	}
    45 	puts("* initialized SDL");
    46 	screen = SDL_SetVideoMode(256, 192, 15, SDL_SWSURFACE);
    47 	if(!screen) {
    48 		puts("# error setting video mode");
    49 		puts(SDL_GetError());
    50 		return 2;
    51 	}
    52 	screen->flags &= ~SDL_PREALLOC;
    53 	puts("* set video mode");
    54 	stick = SDL_JoystickOpen(0);
    55 	if(stick == NULL) {
    56 		puts("# error opening joystick");
    57 		puts(SDL_GetError());
    58 //		return 3;
    59 	}
    60 	puts("* opened joystick");
    61 
    62 	/*splash(screen, 3);*/
    63 
    64 	SDL_FillRect(screen, &rect, RGB15(0,0,31)|0x8000);
    65 	SDL_Flip(screen);
    66 
    67 	while(1)
    68 	while(SDL_PollEvent(&event))
    69 	switch(event.type) {
    70 		case SDL_JOYBUTTONDOWN:
    71 			SDL_FillRect(screen, &rect, (u16)rand()|0x8000);
    72 			SDL_Flip(screen);
    73 			if(rect.w > 8) {
    74 				rect.x += 4; rect.y += 3;
    75 				rect.w -= 8; rect.h -= 6;
    76 			}
    77 			printf("button %d pressed at %d ticks\n",
    78 			       event.jbutton.which, SDL_GetTicks());
    79 		break;
    80 		case SDL_QUIT: SDL_Quit(); return 0;
    81 		default: break;
    82 	}
    83 
    84 	return 0;
    85 }