SDL_Surface* sdl_surface_from_file(const std::string& file, int use_alpha) { SDL_Surface* sdl_surface; SDL_Surface* temp; temp = IMG_Load(file.c_str()); if (temp == NULL) st_abort("Can't load", file); if(use_alpha == IGNORE_ALPHA && !use_gl) sdl_surface = SDL_DisplayFormat(temp); else sdl_surface = SDL_DisplayFormatAlpha(temp); if (sdl_surface == NULL) st_abort("Can't covert to display format", file); if (use_alpha == IGNORE_ALPHA && !use_gl) SDL_SetAlpha(sdl_surface, 0, 0); SDL_FreeSurface(temp); return sdl_surface; }
SDL_Surface* sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha) { SDL_Rect src; SDL_Surface * sdl_surface; SDL_Surface * temp; SDL_Surface * conv; temp = IMG_Load(file.c_str()); if (temp == NULL) { printf("E %s\n", IMG_GetError()); st_abort("Can't load", file); } /* Set source rectangle for conv: */ src.x = x; src.y = y; src.w = w; src.h = h; conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel, temp->format->Rmask, temp->format->Gmask, temp->format->Bmask, temp->format->Amask); /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); #else 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); #endif*/ SDL_SetAlpha(temp,0,0); SDL_BlitSurface(temp, &src, conv, NULL); if(use_alpha == IGNORE_ALPHA && !use_gl) sdl_surface = SDL_DisplayFormat(conv); else sdl_surface = SDL_DisplayFormatAlpha(conv); if (sdl_surface == NULL) st_abort("Can't covert to display format", file); if (use_alpha == IGNORE_ALPHA && !use_gl) SDL_SetAlpha(sdl_surface, 0, 0); SDL_FreeSurface(temp); SDL_FreeSurface(conv); return sdl_surface; }
Surface* Surface::CaptureScreen() { Surface *cap_screen; if (!(screen->flags & SDL_OPENGL)) { cap_screen = new Surface(SDL_GetVideoSurface(),false); } #ifndef NOOPENGL if (use_gl) { SDL_Surface *temp; unsigned char *pixels; int i; temp = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h, 24, #if SDL_BYTEORDER == SDL_LIL_ENDIAN 0x000000FF, 0x0000FF00, 0x00FF0000, 0 #else 0x00FF0000, 0x0000FF00, 0x000000FF, 0 #endif ); if (temp == NULL) st_abort("Error while trying to capture the screen in OpenGL mode",""); pixels = (unsigned char*) malloc(3 * screen->w * screen->h); if (pixels == NULL) { SDL_FreeSurface(temp); st_abort("Error while trying to capture the screen in OpenGL mode",""); } glReadPixels(0, 0, screen->w, screen->h, GL_RGB, GL_UNSIGNED_BYTE, pixels); for (i=0; i<screen->h; i++) memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3*screen->w * (screen->h-i-1), screen->w*3); free(pixels); cap_screen = new Surface(temp,false); SDL_FreeSurface(temp); } #endif return cap_screen; }
Mix_Chunk* load_sound(const std::string& file) { if(!audio_device) return 0; Mix_Chunk* snd = Mix_LoadWAV(file.c_str()); if (snd == 0) st_abort("Can't load", file); return(snd); }
MusicRef MusicManager::load_music(const std::string& file) { if(!audio_device) return MusicRef(0); if(!exists_music(file)) st_abort("Couldn't load musicfile ", file.c_str()); std::map<std::string, MusicResource>::iterator i = musics.find(file); assert(i != musics.end()); return MusicRef(& (i->second)); }
Sprite::Sprite(lisp_object_t* cur) { init_defaults(); LispReader reader(cur); if(!reader.read_string("name", &name)) st_abort("Sprite wihtout name", ""); reader.read_int("x-hotspot", &x_hotspot); reader.read_int("y-hotspot", &y_hotspot); reader.read_float("fps", &fps); std::vector<std::string> images; if(!reader.read_string_vector("images", &images)) st_abort("Sprite contains no images: ", name.c_str()); for(std::vector<std::string>::size_type i = 0; i < images.size(); ++i) { surfaces.push_back( new Surface(datadir + "/images/" + images[i], USE_ALPHA)); } frame_delay = 1000.0f/fps; }
void World::activate_particle_systems() { if (level->particle_system == "clouds") { particle_systems.push_back(new CloudParticleSystem); } else if (level->particle_system == "snow") { particle_systems.push_back(new SnowParticleSystem); } else if (level->particle_system != "") { st_abort("unknown particle system specified in level", ""); } }
SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_) : type(SURFACE), surface(0), use_alpha(use_alpha_) { // Copy the given surface and make sure that it is not stored in // video memory surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE), temp->w, temp->h, temp->format->BitsPerPixel, temp->format->Rmask, temp->format->Gmask, temp->format->Bmask, temp->format->Amask); if(!surface) st_abort("No memory left.", ""); SDL_SetAlpha(temp,0,0); SDL_BlitSurface(temp, NULL, surface, NULL); }
SDL_Surface* sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha) { SDL_Surface* sdl_surface; Uint32 saved_flags; Uint8 saved_alpha; /* Save the alpha blending attributes */ saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); saved_alpha = sdl_surf->format->alpha; if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { SDL_SetAlpha(sdl_surf, 0, 0); } if(use_alpha == IGNORE_ALPHA && !use_gl) sdl_surface = SDL_DisplayFormat(sdl_surf); else sdl_surface = SDL_DisplayFormatAlpha(sdl_surf); /* Restore the alpha blending attributes */ if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha); } if (sdl_surface == NULL) st_abort("Can't covert to display format", "SURFACE"); if (use_alpha == IGNORE_ALPHA && !use_gl) SDL_SetAlpha(sdl_surface, 0, 0); return sdl_surface; }
void GameSession::process_events() { if (end_sequence != NO_ENDSEQUENCE) { Player& tux = *world->get_tux(); tux.input.fire = UP; tux.input.left = UP; tux.input.right = DOWN; tux.input.down = UP; if (int(last_x_pos) == int(tux.base.x)) tux.input.up = DOWN; else tux.input.up = UP; last_x_pos = tux.base.x; SDL_Event event; while (SDL_PollEvent(&event)) { /* Check for menu-events, if the menu is shown */ if (Menu::current()) { Menu::current()->event(event); if(!Menu::current()) st_pause_ticks_stop(); } switch(event.type) { case SDL_QUIT: /* Quit event - quit: */ st_abort("Received window close", ""); break; case SDL_KEYDOWN: /* A keypress! */ { SDLKey key = event.key.keysym.sym; switch(key) { case SDLK_ESCAPE: /* Escape: Open/Close the menu: */ on_escape_press(); break; default: break; } } case SDL_JOYBUTTONDOWN: if (event.jbutton.button == joystick_keymap.start_button) on_escape_press(); break; } } } else // normal mode { if(!Menu::current() && !game_pause) st_pause_ticks_stop(); SDL_Event event; while (SDL_PollEvent(&event)) { /* Check for menu-events, if the menu is shown */ if (Menu::current()) { Menu::current()->event(event); if(!Menu::current()) st_pause_ticks_stop(); /* Tell Tux that the keys are all down, otherwise it could have nasty bugs, like going allways to the right or whatever that key does */ Player& tux = *world->get_tux(); tux.key_event((SDLKey)keymap.jump, UP); tux.key_event((SDLKey)keymap.duck, UP); tux.key_event((SDLKey)keymap.left, UP); tux.key_event((SDLKey)keymap.right, UP); tux.key_event((SDLKey)keymap.fire, UP); } else { Player& tux = *world->get_tux(); switch(event.type) { case SDL_QUIT: /* Quit event - quit: */ st_abort("Received window close", ""); break; case SDL_KEYDOWN: /* A keypress! */ { SDLKey key = event.key.keysym.sym; if(tux.key_event(key,DOWN)) break; switch(key) { case SDLK_ESCAPE: /* Escape: Open/Close the menu: */ on_escape_press(); break; default: break; } } break; case SDL_KEYUP: /* A keyrelease! */ { SDLKey key = event.key.keysym.sym; if(tux.key_event(key, UP)) break; switch(key) { case SDLK_p: if(!Menu::current()) { if(game_pause) { game_pause = false; st_pause_ticks_stop(); } else { game_pause = true; st_pause_ticks_start(); } } break; case SDLK_TAB: if(debug_mode) { tux.size = !tux.size; if(tux.size == BIG) { tux.base.height = 64; } else tux.base.height = 32; } break; case SDLK_END: if(debug_mode) player_status.distros += 50; break; case SDLK_DELETE: if(debug_mode) tux.got_coffee = 1; break; case SDLK_INSERT: if(debug_mode) tux.invincible_timer.start(TUX_INVINCIBLE_TIME); break; case SDLK_l: if(debug_mode) --player_status.lives; break; case SDLK_s: if(debug_mode) player_status.score += 1000; case SDLK_f: if(debug_fps) debug_fps = false; else debug_fps = true; break; default: break; } } break; case SDL_JOYAXISMOTION: if (event.jaxis.axis == joystick_keymap.x_axis) { if (event.jaxis.value < -joystick_keymap.dead_zone) { tux.input.left = DOWN; tux.input.right = UP; } else if (event.jaxis.value > joystick_keymap.dead_zone) { tux.input.left = UP; tux.input.right = DOWN; } else { tux.input.left = DOWN; tux.input.right = DOWN; } } else if (event.jaxis.axis == joystick_keymap.y_axis) { if (event.jaxis.value > joystick_keymap.dead_zone) tux.input.down = DOWN; else if (event.jaxis.value < -joystick_keymap.dead_zone) tux.input.down = UP; else tux.input.down = UP; } break; case SDL_JOYBUTTONDOWN: if (event.jbutton.button == joystick_keymap.a_button) tux.input.up = DOWN; else if (event.jbutton.button == joystick_keymap.b_button) tux.input.fire = DOWN; else if (event.jbutton.button == joystick_keymap.start_button) on_escape_press(); break; case SDL_JOYBUTTONUP: if (event.jbutton.button == joystick_keymap.a_button) tux.input.up = UP; else if (event.jbutton.button == joystick_keymap.b_button) tux.input.fire = UP; break; default: break; } /* switch */ } } /* while */ } }