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