Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly use SDL_BlitSurface() with SDL_CreateRGBSurface()?
    primarykey
    data
    text
    <p>(See "Edit 2" below for the solution.)</p> <p>I need to create SDL surfaces from scratch, instead of loading them from a file. Unfortunately, <code>SDL_BlitSurface()</code> seems to render all colors as black when used with the surface generated through <code>SDL_CreateRGBSurface()</code>. This is my code:</p> <pre><code>int main(int argc, char** argv) { SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE); SDL_Surface* layer = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 100, screen-&gt;format-&gt;BitsPerPixel, screen-&gt;format-&gt;Rmask, screen-&gt;format-&gt;Gmask, screen-&gt;format-&gt;Bmask, screen-&gt;format-&gt;Amask ); SDL_Rect rect; rect.x = 0; rect.y = 0; rect.w = 100; rect.h = 100; Uint32 blue = SDL_MapRGB(screen-&gt;format, 0, 0, 255); SDL_FillRect(layer, &amp;rect, blue); SDL_BlitSurface(screen, NULL, layer, NULL); SDL_Flip(screen); SDL_Delay(3000); return 0; } </code></pre> <p>What I get is a black screen, instead of a 100x100 blue rectangle. What I could find by Googling doesn't seem to help me, as those questions either apply to 8bit surfaces (and setting palettes — my bpp is 32 here) or are left unanswered.</p> <p>So, I would like to know how should I properly blit a generated surface onto a SDL screen.</p> <p><strong>Edit:</strong> I see it was an error in the parameter ordering. The line in question should read</p> <pre><code>SDL_BlitSurface(layer, NULL, screen, NULL); </code></pre> <p>Still, I am having trouble to achieve the same effect in my more complex C++ program. I will post the relevant parts of the code here:</p> <p>main.cpp:</p> <pre><code>int main(int argc, char** argv) { SDLScreen screen(1024, 700, "Hello, SDL!"); SDL_Event event; SDLMenu menu; bool shouldQuit = false; menu.setBounds(200, 100, 200, 600); menu.setFontName("NK211.otf"); menu.setFontSize(36); menu.setEffect(sdlteShadowText); menu.addItem("New game"); menu.addItem("Load game"); menu.addItem("Save game"); menu.addItem("Exit"); menu.render(); while (!shouldQuit) { menu.draw(screen.getSurface()); SDL_Flip(screen.getSurface()); SDL_Delay(10); while (SDL_PollEvent(&amp;event)) { if (event.type == SDL_QUIT) { shouldQuit = true; } else if (event.type == SDL_KEYUP) { if (event.key.keysym.sym == SDLK_q) { shouldQuit = true; } } } } } </code></pre> <p>SDLMenu.cpp:</p> <pre><code>void SDLMenu::setSelectionColorRGB(int r, int g, int b) { SDL_VideoInfo* info = (SDL_VideoInfo*)SDL_GetVideoInfo(); selectionColor = SDL_MapRGB(info-&gt;vfmt, r, g, b); } void SDLMenu::render() { SDLText* current = NULL; SDL_VideoInfo* info = (SDL_VideoInfo*)SDL_GetVideoInfo(); if (!items-&gt;empty()) { current = getItemAt(currentItem); selectionRect = getItemRect(current); setSelectionColorRGB(0,0,255); selectionCanvas = SDL_CreateRGBSurface(SDL_HWSURFACE, selectionRect-&gt;w, selectionRect-&gt;h, info-&gt;vfmt-&gt;BitsPerPixel, info-&gt;vfmt-&gt;Rmask, info-&gt;vfmt-&gt;Gmask, info-&gt;vfmt-&gt;Bmask, info-&gt;vfmt-&gt;Amask); SDL_FillRect(selectionCanvas, selectionRect, selectionColor); SDL_SaveBMP(selectionCanvas, "selection.bmp"); // debug } for (list&lt;SDLText*&gt;::iterator i = items-&gt;begin(); i != items-&gt;end(); i++) { (*i)-&gt;render(); } } void SDLMenu::draw(SDL_Surface* canvas) { int currentY = bounds.y; if (selectionCanvas != NULL) { SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect); } for (list&lt;SDLText*&gt;::iterator i = items-&gt;begin(); i != items-&gt;end(); i++) { (*i)-&gt;draw(bounds.x, currentY, canvas); currentY += fontSize + itemGap; } } </code></pre> <p>SDLScreen.cpp:</p> <pre><code>SDLScreen::SDLScreen(int w, int h, string t, int d) : width(w), height(h), depth(d), title(t) { SDL_Init(SDL_INIT_EVERYTHING); SDL_WM_SetCaption(title.c_str(), NULL); refresh(); } void SDLScreen::refresh() { screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE); } </code></pre> <p>The selection rectangle for the active menu item should be blue, but it shows up in black. The file <code>selection.bmp</code> is also all black.</p> <p><strong>Edit 2:</strong> I found out what created the problem. The <code>selectionRect</code> was set relative to the screen, while the <code>selectionCanvas</code> had the width and height of a particular menu item. So, the filling was done out of bounds of the <code>selectionCanvas</code>. Adding separate <code>SDL_Rect</code> for filling solved the problem.</p> <pre><code>SDL_Rect fillRect; fillRect.x = 0; fillRect.y = 0; fillRect.w = selectionRect-&gt;w; fillRect.h = selectionRect-&gt;h; SDL_FillRect(selectionCanvas, &amp;fillRect, selectionColor); // and later... SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload