c++ - SDL UpdateWindowSurface() returns -1 if called from a class (in separate file) -
today started c++/sdl2 snake clone, , i've been looking ways make code neater, -particularly classes. tried put sdl code used window/display in class. when run code, the window closes instantly. error tests set in display.cpp, tells me through console sdl_updatewindowsurface() (in display.update()) returning -1. why happen when rearrange code this?
with code load image in main() , display through class' function applysurface(). idea have classes/objects game's grid/board, snake, etc., -each calling applysurface() own images. feel free tell me if bad idea altogether.
main.cpp:
#include <sdl.h> #include "display.h" sdl_event event; sdl_surface* image = nullptr; int main(int argc, char* args[]) { display display; display.loadimage("image.bmp"); if (sdl_init(sdl_init_everything) == -1) return false; bool quit = false; while (!quit) { while (sdl_pollevent(&event)) { if (event.type == sdl_quit) quit = true; } display.applysurface(0, 0, image, display.windowsurface); display.update(); } sdl_freesurface(image); sdl_quit(); return 0; }
display.h:
#pragma once #include <sdl.h> #include <string> #include <iostream> class display { public: sdl_window* window; sdl_surface* windowsurface; display(); sdl_surface *loadimage(std::string filename); void applysurface(int x, int y, sdl_surface *source, sdl_surface *destination, sdl_rect *clip = nullptr); void update(); ~display(); private: const int window_width = 612; const int window_height = 632; const int screen_bpp = 2; };
display.cpp:
#pragma once #include "display.h" display::display() { window = sdl_createwindow("snake", sdl_windowpos_centered, sdl_windowpos_centered, window_width, window_height, sdl_window_shown); if (window == null) std::cout << "error: sdl_createwindow failed." << std::endl; windowsurface = sdl_getwindowsurface(window); } sdl_surface* display::loadimage(std::string filename) { sdl_surface* loadedimage = null; sdl_surface* optimizedimage = null; loadedimage = sdl_loadbmp(filename.c_str()); if (loadedimage != null) { optimizedimage = sdl_convertsurface(loadedimage, windowsurface->format, 0); sdl_freesurface(loadedimage); if (optimizedimage != null) sdl_setcolorkey(optimizedimage, sdl_true, sdl_maprgb(optimizedimage->format, 255, 255, 255)); } return optimizedimage; } void display::applysurface(int x, int y, sdl_surface *source, sdl_surface *destination, sdl_rect *clip) { sdl_rect offset; offset.x = x; offset.y = y; sdl_blitsurface(source, clip, destination, &offset); } void display::update() { if (sdl_updatewindowsurface(window) == -1) std::cout << "error: sdl_updatewindowsurface() failed." << std::endl; } display::~display() { sdl_freesurface(windowsurface); windowsurface = null; sdl_destroywindow(window); window = null; }
this valid use of classes structure code. sdl_init
needs come before other sdl functions, means you're best off moving sdl_init
top of main or adding display constructor. if add beginning of display constructor means can have 1 display class object running @ time, fine in case.
Comments
Post a Comment