twindow::twindow(const std::string& title, const int x, const int y, const int w, const int h, const Uint32 window_flags, const Uint32 render_flags) : window_(SDL_CreateWindow(title.c_str(), x, y, w, h, window_flags)) , pixel_format_(SDL_PIXELFORMAT_UNKNOWN) { if(!window_) { throw texception("Failed to create a SDL_Window object.", true); } if(!SDL_CreateRenderer(window_, -1, render_flags)) { throw texception("Failed to create a SDL_Renderer object.", true); } SDL_RendererInfo info; if(SDL_GetRendererInfo(*this, &info) != 0) { throw texception("Failed to retrieve the information of the renderer.", true); } if(info.num_texture_formats == 0) { throw texception("The renderer has no texture information available.\n", false); } pixel_format_ = info.texture_formats[0]; fill(0,0,0); render(); }
ttexture::ttexture(SDL_Renderer& renderer, const int access, const surface& surface) : reference_count_(new unsigned(1)) , texture_(NULL) , rotation_(0) , hscale_(1) , vscale_(1) , smooth_scaling_(false) , flip_(SDL_FLIP_NONE) , clip_() , mod_r_(255) , mod_g_(255) , mod_b_(255) , alpha_(255) , source_surface_( SDL_ConvertSurface(surface, surface->format, surface->flags)) { if(source_surface_ == NULL) { throw texception("Invalid source_surface__ argument passed, failed to " "create SDL_Texture object.", false); } initialise_from_surface(renderer, access); }
void twindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a) { SDL_SetRenderDrawColor(*this, r, g, b, a); if(SDL_RenderClear(*this) != 0) { throw texception("Failed to clear the SDL_Renderer object.", true); } }
void ttexture_lock::stream_surface(SDL_Surface *surf) { int h; if (SDL_QueryTexture(texture_, NULL, NULL, NULL, &h) != 0) { throw texception("Could not query a texture", true); } memcpy(pixels, surf->pixels, pitch * h); }
surface blit_modification::operator()(const surface& src) const { if(x_ >= src->w) { std::stringstream sstr; sstr << "~BLIT(): x-coordinate '" << x_ << "' larger than destination image's width '" << src->w << "' no blitting performed.\n"; throw texception(sstr); } if(y_ >= src->h) { std::stringstream sstr; sstr << "~BLIT(): y-coordinate '" << y_ << "' larger than destination image's height '" << src->h << "' no blitting performed.\n"; throw texception(sstr); } if(surf_->w + x_ > src->w) { std::stringstream sstr; sstr << "~BLIT(): offset and width '" << x_ + surf_->w << "' larger than destination image's width '" << src->w << "' no blitting performed.\n"; throw texception(sstr); } if(surf_->h + y_ > src->h) { std::stringstream sstr; sstr << "~BLIT(): offset and height '" << y_ + surf_->h << "' larger than destination image's height '" << src->h << "' no blitting performed.\n"; throw texception(sstr); } //blit_surface want neutral surfaces surface nsrc = make_neutral_surface(src); surface nsurf = make_neutral_surface(surf_); SDL_Rect r = create_rect(x_, y_, 0, 0); blit_surface(nsurf, NULL, nsrc, &r); return nsrc; }
ttexture::ttexture(SDL_Renderer& renderer, const int access, const std::string& file) : reference_count_(new unsigned(1)) , texture_(NULL) , source_surface_(IMG_Load(file.c_str())) { if(source_surface_ == NULL) { throw texception("Failed to create SDL_Texture object.", true); } if(access == SDL_TEXTUREACCESS_STATIC) { texture_ = SDL_CreateTextureFromSurface(&renderer, source_surface_); if(texture_ == NULL) { throw texception("Failed to create SDL_Texture object.", true); } SDL_FreeSurface(source_surface_); source_surface_ = NULL; } else if(access == SDL_TEXTUREACCESS_STREAMING) { texture_ = SDL_CreateTexture(&renderer, source_surface_->format->format, SDL_TEXTUREACCESS_STREAMING, source_surface_->w, source_surface_->h); if(texture_ == NULL) { throw texception("Failed to create SDL_Texture object.", true); } const int update = SDL_UpdateTexture(texture_, NULL, source_surface_->pixels, source_surface_->pitch); if(update != 0) { throw texception("Failed to update the SDL_Texture object during " "its construction.", true); } } else { throw texception("Unknown texture access mode.", false); } }
ttexture::ttexture(SDL_Renderer& renderer, const Uint32 format, const int access, const int w, const int h) : reference_count_(new unsigned(1)) , texture_(SDL_CreateTexture(&renderer, format, access, w, h)) , source_surface_(NULL) { if(!texture_) { throw texception("Failed to create a SDL_Texture object.", true); } }
void ttexture::initialise_from_surface(SDL_Renderer& renderer, const int access) { if(access == SDL_TEXTUREACCESS_STATIC) { texture_ = SDL_CreateTextureFromSurface(&renderer, source_surface_); if(texture_ == NULL) { throw texception("Failed to create SDL_Texture object.", true); } clip_ = create_rect(0, 0, source_surface_->w, source_surface_->h); SDL_FreeSurface(source_surface_); source_surface_ = NULL; } else if(access == SDL_TEXTUREACCESS_STREAMING) { texture_ = SDL_CreateTexture(&renderer, source_surface_->format->format, SDL_TEXTUREACCESS_STREAMING, source_surface_->w, source_surface_->h); if(texture_ == NULL) { throw texception("Failed to create SDL_Texture object.", true); } clip_ = create_rect(0, 0, source_surface_->w, source_surface_->h); const int update = SDL_UpdateTexture(texture_, NULL, source_surface_->pixels, source_surface_->pitch); if(update != 0) { throw texception("Failed to update the SDL_Texture object during " "its construction.", true); } } else { throw texception("Unknown texture access mode.", false); } SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND); }
void ttexture::update_pixels(SDL_Surface *surf) { const int retcode = SDL_UpdateTexture(texture_, NULL, surf->pixels, surf->pitch); if (retcode != 0) { throw texception("Failed to update SDL_Texture object.", true); } if (source_surface_ != NULL) { SDL_FreeSurface(source_surface_); source_surface_ = surf; } }
ttexture::ttexture(SDL_Renderer& renderer, const int access, const std::string& file) : reference_count_(new unsigned(1)) , texture_(NULL) , rotation_(0) , hscale_(1) , vscale_(1) , smooth_scaling_(false) , flip_(SDL_FLIP_NONE) , clip_() , mod_r_(255) , mod_g_(255) , mod_b_(255) , alpha_(255) , source_surface_(IMG_Load(file.c_str())) { if(source_surface_ == NULL) { throw texception("Failed to create SDL_Texture object.", true); } initialise_from_surface(renderer, access); }
ttexture::ttexture(SDL_Renderer& renderer, const Uint32 format, const int access, const int w, const int h) : reference_count_(new unsigned(1)) , texture_(SDL_CreateTexture(&renderer, format, access, w, h)) , rotation_(0) , hscale_(1) , vscale_(1) , smooth_scaling_(false) , flip_(SDL_FLIP_NONE) , clip_(create_rect(0, 0, w, h)) , mod_r_(255) , mod_g_(255) , mod_b_(255) , alpha_(255) , source_surface_(NULL) { SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND); if(!texture_) { throw texception("Failed to create a SDL_Texture object.", true); } }
void ttexture_lock::lock() { if (SDL_LockTexture(texture_, NULL, &pixels, &pitch) != 0) { throw texception("Could not lock a texture", true); } }