SDL_Surface* draw_line_to(const PointI& source, const PointI& target, const SDL_Color& colour){ using namespace Surface_Tools; SDL_Surface* surface = NULL; PointI p1, p2; if(source.x < target.x) { p1.x = source.x; p2.x = target.x; } else { p1.x = target.x; p2.x = source.x; } if(source.y < target.y) { p1.y = source.y; p2.y = target.y; } else { p1.y = target.y; p2.y = source.y; } surface = create_blank_surface(p2 - p1); bool steep = abs(p2.y - p1.y) > abs(p2.x - p1.x); if(steep) { std::swap(p1.x, p1.y); std::swap(p2.x, p2.y); } if(p1.x > p2.x) { std::swap(p1.x, p2.x); std::swap(p1.y, p2.y); } int delta_x = p2.x - p1.x; int delta_y = abs(p2.y - p1.y); int error = delta_x / 2; int y_step = 0; int y = p1.y; y_step = p1.y < p2.y ? 1 : -1; for(int x = p1.x; x < p2.x; ++x) { if(steep) { set_pixel(surface, y, x, colour); } else { set_pixel(surface, x, y, colour); } error = error - delta_y; if(error < 0) { y += y_step; error += delta_x; } } return surface;}
int main(int argc, char* argv[]){ Logger::Initialise("log.txt"); SDL_Systems::Initialise_SDL(); SDL_Systems::Initialise_Mixer(44100, MIX_DEFAULT_FORMAT, 2, 4096); Surface_Cache::Initialise(SDL_Tools::Colours::WHITE); SDL_Renderer renderer(640, 480, 24, "Test", SDL_Tools::Colours::BLACK); SDL_Surface* x = draw_line_to( to_point_type<float, int>(PointF(10.0f, 10.0f)), to_point_type<float, int>(PointF(30.0f, 20.0f)), SDL_Tools::colour(255, 0, 0)); Surface missile_sfc("missile.png", false);
After that function is called, any other attempts to load images with SDL fails, yielding "Out of memory" errors when SDL_GetError is called. I suspected it may have been the function below, but I am not sure:
SDL_Surface* Surface_Tools::create_blank_surface(const PointI& size, int depth){ using namespace SDL_Tools; SDL_Surface* sfc = SDL_CreateRGBSurface(SDL_SWSURFACE, size.x, size.y, depth, r_mask, g_mask, b_mask, a_mask); if(!sfc) { throw LRE_Exception("SDL_CreateRGBSurface failed in create_blank_surface: " + std::string(SDL_GetError())); } SDL_Surface* ret = SDL_DisplayFormat(sfc); SDL_FreeSurface(sfc); if(!ret) { throw LRE_Exception("Could not create surface in create_blank_surface: " + std::string(SDL_GetError())); } return ret;}
Is there anything fishy with my SDL code that could cause this problem? I am initialising SDL before doing any work with SDL.
Cheers.