int main(int argc, char* argv[]) { ballfield_t *balls; SDL_Surface *screen; SDL_Surface *temp_image; SDL_Surface *back, *logo, *font; SDL_Event event; int bpp = 0, flags = SDL_DOUBLEBUF | SDL_SWSURFACE, alpha = 1; int x_offs = 0, y_offs = 0; long tick, last_tick, last_avg_tick; double t = 0; float dt; int i; float fps = 0.0; int fps_count = 0; int fps_start = 0; float x_speed, y_speed, z_speed; SDL_Init(SDL_INIT_VIDEO); atexit(SDL_Quit); for(i = 1; i < argc; ++i) { if(strncmp(argv[i], "-na", 3) == 0) alpha = 0; else if(strncmp(argv[i], "-nd", 3) == 0) flags &= ~SDL_DOUBLEBUF; else if(strncmp(argv[i], "-h", 2) == 0) { flags |= SDL_HWSURFACE; flags &= ~SDL_SWSURFACE; } else if(strncmp(argv[i], "-f", 2) == 0) flags |= SDL_FULLSCREEN; else bpp = atoi(&argv[i][1]); } screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, bpp, flags); if(!screen) { fprintf(stderr, "Failed to open screen!\n"); exit(-1); } SDL_WM_SetCaption("Ballfield", "Ballfield"); if(flags & SDL_FULLSCREEN) SDL_ShowCursor(0); balls = ballfield_init(); if(!balls) { fprintf(stderr, "Failed to create ballfield!\n"); exit(-1); } /* * Load and prepare balls... */ balls->use_alpha = alpha; if( ballfield_load_gfx(balls, "bmp/blueball.png", 0) || ballfield_load_gfx(balls, "bmp/redball.png", 1) ) { fprintf(stderr, "Could not load balls!\n"); exit(-1); } /* * Load background image */ temp_image = IMG_Load("bmp/redbluestars.png"); if(!temp_image) { fprintf(stderr, "Could not load background!\n"); exit(-1); } back = SDL_DisplayFormat(temp_image); SDL_FreeSurface(temp_image); /* * Load logo */ temp_image = SDL_LoadBMP("bmp/logo.bmp"); if(!temp_image) { fprintf(stderr, "Could not load logo!\n"); exit(-1); } SDL_SetColorKey(temp_image, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(temp_image->format, 255, 0, 255)); logo = SDL_DisplayFormat(temp_image); SDL_FreeSurface(temp_image); /* * Load font */ temp_image = SDL_LoadBMP("bmp/font7x10.bmp"); if(!temp_image) { fprintf(stderr, "Could not load font!\n"); exit(-1); } SDL_SetColorKey(temp_image, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(temp_image->format, 255, 0, 255)); font = SDL_DisplayFormat(temp_image); SDL_FreeSurface(temp_image); last_avg_tick = last_tick = SDL_GetTicks(); while(1) { SDL_Rect r; if(SDL_PollEvent(&event) > 0) { if(event.type == SDL_MOUSEBUTTONDOWN) break; if(event.type & (SDL_KEYUP | SDL_KEYDOWN)) { Uint8 *keys = SDL_GetKeyState(&i); if(keys[SDLK_ESCAPE]) break; } } /* Timing */ tick = SDL_GetTicks(); dt = (tick - last_tick) * 0.001f; last_tick = tick; /* Background image */ tiled_back(back, screen, x_offs>>11, y_offs>>11); /* Ballfield */ ballfield_render(balls, screen); /* Logo */ r.x = 2; r.y = 2; SDL_BlitSurface(logo, NULL, screen, &r); /* FPS counter */ if(tick > fps_start + 500) { fps = (float)fps_count * 1000.0 / (tick - fps_start); fps_count = 0; fps_start = tick; } print_num(screen, font, screen->w-37, screen->h-12, fps); ++fps_count; SDL_Flip(screen); /* Animate */ x_speed = 500.0 * sin(t * 0.37); y_speed = 500.0 * sin(t * 0.53); z_speed = 400.0 * sin(t * 0.21); ballfield_move(balls, x_speed, y_speed, z_speed); x_offs -= x_speed; y_offs -= y_speed; t += dt; } ballfield_free(balls); SDL_FreeSurface(back); SDL_FreeSurface(logo); SDL_FreeSurface(font); exit(0); }
int main(int argc, char* argv[]) { ballfield_t *balls; SDL_Surface *screen; SDL_Surface *temp_image; SDL_Surface *back, *logo, *font, *font_hex; SDL_Event event; int bpp = 32, flags = 0, alpha = 1; int x_offs = 0, y_offs = 0; long tick, last_tick, last_avg_tick; double t = 0; float dt; int i; float fps = 0.0; int fps_count = 0; int fps_start = 0; float x_speed, y_speed, z_speed; SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK); atexit(SDL_Quit); screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, bpp, flags); if(!screen) { fprintf(stderr, "Failed to open screen!\n"); exit(-1); } SDL_WM_SetCaption("Ballfield", "Ballfield"); if(flags & SDL_FULLSCREEN) SDL_ShowCursor(0); balls = ballfield_init(); if(!balls) { fprintf(stderr, "Failed to create ballfield!\n"); exit(-1); } /* * Load and prepare balls... */ balls->use_alpha = alpha; if( ballfield_load_gfx(balls, "blueball.png", 0) || ballfield_load_gfx(balls, "redball.png", 1) ) { fprintf(stderr, "Could not load balls!\n"); exit(-1); } /* * Load background image */ temp_image = IMG_Load("sun.gif"); if(!temp_image) { fprintf(stderr, "Could not load background!\n"); exit(-1); } back = SDL_DisplayFormat(temp_image); SDL_FreeSurface(temp_image); /* * Load logo */ temp_image = SDL_LoadBMP("logo.bmp"); if(!temp_image) { fprintf(stderr, "Could not load logo!\n"); exit(-1); } SDL_SetColorKey(temp_image, SDL_SRCCOLORKEY, SDL_MapRGB(temp_image->format, 255, 0, 255)); logo = SDL_DisplayFormat(temp_image); SDL_FreeSurface(temp_image); /* * Load font */ temp_image = SDL_LoadBMP("font7x10.bmp"); if(!temp_image) { fprintf(stderr, "Could not load font!\n"); exit(-1); } SDL_SetColorKey(temp_image, SDL_SRCCOLORKEY, SDL_MapRGB(temp_image->format, 255, 0, 255)); font = SDL_DisplayFormat(temp_image); SDL_FreeSurface(temp_image); temp_image = SDL_LoadBMP("font7x10-hex.bmp"); if(!temp_image) { fprintf(stderr, "Could not load hex font!\n"); exit(-1); } SDL_SetColorKey(temp_image, SDL_SRCCOLORKEY, SDL_MapRGB(temp_image->format, 255, 0, 255)); font_hex = SDL_DisplayFormat(temp_image); SDL_FreeSurface(temp_image); last_avg_tick = last_tick = SDL_GetTicks(); enum { MAX_POINTERS = 16, PTR_PRESSED = 4 }; int touchPointers[MAX_POINTERS][5]; memset(touchPointers, 0, sizeof(touchPointers)); SDL_Joystick * joysticks[MAX_POINTERS+1]; for(i=0; i<MAX_POINTERS; i++) joysticks[i] = SDL_JoystickOpen(i); while(1) { SDL_Rect r; /* Timing */ tick = SDL_GetTicks(); dt = (tick - last_tick) * 0.001f; last_tick = tick; if( bpp == 32 ) SDL_FillRect(screen, NULL, 0); // Clear alpha channel /* Background image */ tiled_back(back, screen, x_offs>>11, y_offs>>11); /* Ballfield */ ballfield_render(balls, screen); /* Logo */ r.x = 2; r.y = 2; SDL_BlitSurface(logo, NULL, screen, &r); /* FPS counter */ if(tick > fps_start + 500) { fps = (float)fps_count * 1000.0 / (tick - fps_start); fps_count = 0; fps_start = tick; } ++fps_count; for(i=0; i<MAX_POINTERS; i++) { if( !touchPointers[i][PTR_PRESSED] ) continue; r.x = touchPointers[i][0]; r.y = touchPointers[i][1]; r.w = 80 + touchPointers[i][2] / 10; // Pressure r.h = 80 + touchPointers[i][3] / 10; // Touch point size r.x -= r.w/2; r.y -= r.h/2; SDL_FillRect(screen, &r, 0xaaaaaa); print_num(screen, font, r.x, r.y, i+1); } int mx, my; int b = SDL_GetMouseState(&mx, &my); Uint32 color = 0xff; if( b ) { color = 0; if( b & SDL_BUTTON_LEFT ) color |= 0xff00; if( b & SDL_BUTTON_RIGHT ) color |= 0xff0000; } r.x = mx; r.y = my; r.w = 30; r.h = 30; r.x -= r.w/2; r.y -= r.h/2; SDL_FillRect(screen, &r, color); SDL_Flip(SDL_GetVideoSurface()); SDL_Event evt; while( SDL_PollEvent(&evt) ) { if(evt.type == SDL_KEYUP || evt.type == SDL_KEYDOWN) { if(evt.key.keysym.sym == SDLK_ESCAPE) return 0; __android_log_print(ANDROID_LOG_INFO, "Ballfield", "SDL key event: evt %s state %s key %d scancode %d mod %d unicode %d", evt.type == SDL_KEYUP ? "UP " : "DOWN" , evt.key.state == SDL_PRESSED ? "PRESSED " : "RELEASED", (int)evt.key.keysym.sym, (int)evt.key.keysym.scancode, (int)evt.key.keysym.mod, (int)evt.key.keysym.unicode); } if(evt.type == SDL_VIDEORESIZE) __android_log_print(ANDROID_LOG_INFO, "Ballfield", "SDL resize event: %d x %d", evt.resize.w, evt.resize.h); if(evt.type == SDL_ACTIVEEVENT) __android_log_print(ANDROID_LOG_INFO, "Ballfield", "======= SDL active event: gain %d state %d", evt.active.gain, evt.active.state); /* if( evt.type == SDL_ACTIVEEVENT && evt.active.gain == 0 && evt.active.state & SDL_APPACTIVE ) { // We've lost GL context, we are not allowed to do any GFX output here, or app will crash! while( 1 ) { SDL_PollEvent(&evt); if( evt.type == SDL_ACTIVEEVENT && evt.active.gain && evt.active.state & SDL_APPACTIVE ) { __android_log_print(ANDROID_LOG_INFO, "Ballfield", "======= SDL active event: gain %d state %d", evt.active.gain, evt.active.state); SDL_Flip(SDL_GetVideoSurface()); // One SDL_Flip() call is required here to restore OpenGL context // Re-load all textures, matrixes and all other GL states if we're in SDL+OpenGL mode // Re-load all images to SDL_Texture if we're using it // Now we can draw break; } // Process network stuff, maybe play some sounds using SDL_ANDROID_PauseAudioPlayback() / SDL_ANDROID_ResumeAudioPlayback() SDL_Delay(300); __android_log_print(ANDROID_LOG_INFO, "Ballfield", "Waiting"); } } */ if( evt.type == SDL_JOYAXISMOTION ) { if( evt.jaxis.which == 0 ) // 0 = The accelerometer continue; int joyid = evt.jaxis.which - 1; touchPointers[joyid][evt.jaxis.axis] = evt.jaxis.value; // Axis 0 and 1 are coordinates, 2 and 3 are pressure and touch point radius } if( evt.type == SDL_JOYBUTTONDOWN || evt.type == SDL_JOYBUTTONUP ) { if( evt.jbutton.which == 0 ) // 0 = The accelerometer continue; int joyid = evt.jbutton.which - 1; touchPointers[joyid][PTR_PRESSED] = (evt.jbutton.state == SDL_PRESSED); } } /* Animate */ x_speed = 500.0 * sin(t * 0.37); y_speed = 500.0 * sin(t * 0.53); z_speed = 400.0 * sin(t * 0.21); ballfield_move(balls, x_speed, y_speed, z_speed); x_offs -= x_speed; y_offs -= y_speed; t += dt; } ballfield_free(balls); SDL_FreeSurface(back); SDL_FreeSurface(logo); SDL_FreeSurface(font); std::ostringstream os; os << "lalala" << std::endl << "more text" << std::endl; std::cout << os.str() << std::endl << "text text" << std::endl; exit(0); }