c++ - SDL, How can I make the background picture render? -
i had render problem, fixed that. problem is, picture won't show up. know picture gets loaded because have error handler if isn't loaded. know code messy, need picture show up.
#include <sdl/sdl.h> #include <stdio.h> #include <iostream> #include <fstream> #include <string> using namespace std; const int fps = 60; sdl_window *window; int main(int argc, char **argv) { sdl_init(sdl_init_everything); window = sdl_createwindow("", 300, 100, 1024, 800, sdl_window_opengl); if (window == null) { cout << ("could not create window: %s/n", sdl_geterror()); return 1; } sdl_renderer *renderer = sdl_createrenderer(window, -1, 0); int x = 0, y = 0; sdl_surface *screen; sdl_surface *background = sdl_loadbmp("hockeyrink.bmp"); if(background == null) { sdl_showsimplemessagebox(0, "background init error", sdl_geterror(), window); } if(renderer == null) { sdl_showsimplemessagebox(0, "renderer init error", sdl_geterror(), window); } sdl_texture *texture = sdl_createtexturefromsurface(renderer,background); if(texture == null) { sdl_showsimplemessagebox(0, "texture init error", sdl_geterror(), window); } const int speed = 5; sdl_rect camera; camera.x = 0; //don't worry camera, need after background working. camera.y = 0; camera.w = 800; camera.h = 600; bool b[2] = {0,0}; uint32 start; bool running = true; while (running) { start = sdl_getticks(); sdl_event event; while(sdl_pollevent(&event)) { switch(event.type) { case sdl_quit: running = false; sdl_destroytexture(texture); sdl_freesurface(background); sdl_destroyrenderer(renderer); sdl_destroywindow(window); sdl_quit(); break; case sdl_keydown: switch(event.key.keysym.sym) { case sdlk_up: b[0]=1; break; case sdlk_left: b[1]=1; break; } break; case sdl_keyup: switch(event.key.keysym.sym) { case sdlk_up: b[0]=0; break; case sdlk_left: b[1]=0; break; } break; } } if(b[0]) { x+=speed; camera.y+=speed; if (camera.y > 3000-800) { camera.y=0; } } else if(b[1]) { x-=speed; camera.y-=speed; if (camera.y <= 0) { camera.y = 2000-800; } sdl_rendercopy(renderer, texture, null, null); sdl_renderpresent(renderer); } if(1000/fps>sdl_getticks()-start) { sdl_delay(1000/fps-(sdl_getticks() - start)); } } return 0; }
you have initialized b[1]
0
, texture rendered when b[1]
1
in code.
should either put lines:
sdl_rendercopy(renderer, texture, null, null); sdl_renderpresent(renderer);
before loop,
or press left key of keyboard(i.e. sdlk_left
)
render , present image on screen.
it works in system, should work in yours, too.
Comments
Post a Comment