C++ Questions (SDL)

Started by
8 comments, last by Zahlman 15 years, 3 months ago
Good evening from germany! :) Some little question because i'am buy me a GP2X (console). I can code game with c++ and the SDL Libary. My optional the last years was VB6. So i have no skills in c++. Only understanding some code snippets. 1. How i can REFRESH a screen? At the moment i draw a rectangle on the screen wich overlap the old screen with: SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b)); There are no method to refresh the new position of a picture and draw only the new position? 2. How blend a picture? I wan't to change the transparency of an image. HOW? 3. There are some other SDL commands for pictures? Like rotate, size, and so on?
Advertisement
1)To refresh the screen you need to call SDL_Flip(screen); You don't do it just once at the begining of the program - you do it every frame, after you finished drawing everything to "screen".

2)You need to use Alpha Blending

3)SDL does not use the GPU for drawing, therefore it can't rotate or scale images. If you want rotation and scaling, you have two options:

I)Download SDL_gfx - a SDL extension library that allows you to do rotation, scaling and graphic effects. However it uses the CPU just like SDL, so rotation and scaling will be quite slow for real time blitting, so those operations should be performed beforehand at load time - aka you save your image at different rotation angles and sizes on the memory.

II)Use OpenGL, and fully utilize the GPU's rotation and scaling powers. SDL has an OpenGL interface, so that shouldn't be a problem.



BTW, why buying a GP2X now? The Pandora team will take second batch preorders soon...
-----------------------------------------Everyboddy need someboddy!
Bear in mind that the GP2X doesn't have an OpenGL implementation (at least one that runs on accelerated hardware).

SDL Tutorials.

Steven Yau
[Blog] [Portfolio]

Yes only SDL is availeble :( ^^

So i try it 3 hours with no result! Please take a look. The image won't be go invisible stepply.

This won't work http://gpwiki.org/index.php/SDL:Tutorials:2D_Graphics

main.c
#include <cstdlib>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <SDL.h>#include <SDL_image.h>#include "render.h"/* GP2X button mapping */enum MAP_KEY{   VK_UP         , // 0   VK_UP_LEFT    , // 1   VK_LEFT       , // 2   VK_DOWN_LEFT  , // 3   VK_DOWN       , // 4   VK_DOWN_RIGHT , // 5   VK_RIGHT      , // 6   VK_UP_RIGHT   , // 7   VK_START      , // 8   VK_SELECT     , // 9   VK_FL         , // 10   VK_FR         , // 11   VK_FA         , // 12   VK_FB         , // 13   VK_FX         , // 14   VK_FY         , // 15   VK_VOL_UP     , // 16   VK_VOL_DOWN   , // 17   VK_TAT          // 18};/* The screen surface, joystick device */SDL_Joystick *joy = NULL;SDL_Event event;int done;int MouseX;int MouseY;int picCount;void Terminate(void){   SDL_Quit();#ifdef GP2X   chdir("/usr/gp2x");   execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);#endif}int main (int argc, char *argv[]){      /* Initialize SDL */   if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {      fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());      exit (1);   }   atexit (SDL_Quit);   SDL_ShowCursor(SDL_DISABLE);      /* Set 320x240 16-bits video mode */   screen = SDL_SetVideoMode (320, 240, 16,  SDL_SWSURFACE);   if (screen == NULL) {      fprintf (stderr, "Couldn't set 320x240x16 video mode: %s\n", SDL_GetError ());      exit (2);   }   /* Check and open joystick device */   if (SDL_NumJoysticks() > 0) {      joy = SDL_JoystickOpen(0);      if(!joy) {         fprintf (stderr, "Couldn't open joystick 0: %s\n", SDL_GetError ());      }   }#ifdef GP2X   /* Only use GP2X code here */#endif#ifdef WIN32   /* Only use Windows code here */#endif   int button, x=50, y=50;   picCount = 0;   done = 0;   while (!done)   {      clearscreen();      if (picCount >= 1) renderpics();             /* Check for events */      while (SDL_PollEvent (&event))      {         switch (event.type)         {            case SDL_JOYBUTTONDOWN:               /* if press Start button, terminate program */               if ( event.jbutton.button == VK_START )                  done = 1;               break;            case SDL_MOUSEMOTION:               MouseX = event.button.x;               MouseY = event.button.y;               targetpos.x = MouseX - 25;               targetpos.y = MouseY - 25;                            case SDL_MOUSEBUTTONDOWN:               MouseX = event.button.x;               MouseY = event.button.y;               targetpos.x = MouseX - 25;               targetpos.y = MouseY - 25;               picCount = picCount + 1;               break;            case SDL_JOYBUTTONUP:               break;            case SDL_QUIT:               done = 1;               break;            default:               break;         }      }      SDL_Flip(screen);   }   return 0;}


render.c
//Bildschirm leeren //SDL_Surface *screen = NULL;SDL_Surface *image = IMG_Load("test.png");Uint8 r, g, b;static void clearscreen(){r = 0;g = 160;b = 255;SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));  }// Render PICS //SDL_Rect targetpos;static void renderpics(){targetpos.x = targetpos.x;targetpos.y = targetpos.y;SDL_BlitSurface(image, NULL, screen, &targetpos);}
I've noticed 3 problems with your code:


1)You didn't do an SDL_Delay call. Delay calls are important because they let the operation system put events in your program's events poll. So no delay calls - no events.

2)I'm not sure about that, but I think you can only load images after you initialize SDL. If you try loading it as a global variable, the IMG_load call will be performed before the SDL_Init call.

3)You need to format the image before you can display it. You need to call SDL_DisplayFormat(or SDL_DisplayFormatAlpha), and the results of that function is the surface you should blit. The old unformatted surface should be freed from the memory right after the formatting.
-----------------------------------------Everyboddy need someboddy!
Quote:
1)You didn't do an SDL_Delay call. Delay calls are important because they let the operation system put events in your program's events poll. So no delay calls - no events.

This is false.

Quote:
2)I'm not sure about that, but I think you can only load images after you initialize SDL. If you try loading it as a global variable, the IMG_load call will be performed before the SDL_Init call.

This is true, and could well be the source of the problem.

Quote:
3)You need to format the image before you can display it. You need to call SDL_DisplayFormat(or SDL_DisplayFormatAlpha), and the results of that function is the surface you should blit. The old unformatted surface should be freed from the memory right after the formatting

You don't need to format it, it isn't an error not to. But it is recommended yes.
Guess I should retire to jackstraws...



DAMN! That joke is so lost in translation...
-----------------------------------------Everyboddy need someboddy!
Next question!

See the Code below and tell me whyhe said !expected `)' before "endung"!

http://www.pic-upload.de/26.12.08/cnnrt3.gif

I have an array wich is int newimage[] = {1,2};
I have an filetype endung[5] = ".png";

Wan't to draw a text with drawText(screen, puffer endung, 3, 23, 0, 0, 0);

puffer is:

IntToString(newimage, puffer);

I wan't an array ... 1,2,3,4, ... and in the for i ... hang on the filetyp ".png" to load an image. in an array.

Merci
You can't combine non-literal strings like that ("puffer endung"). Since this is C++, try this instead:

#include <sstream>
std::ostringstream ss;
ss << newimage << endung;
drawText( screen, ss.str().to_str(), 3, 23, 0, 0, 0 );

Instead of your current IntToString and drawText.
Quote:Original post by MaulingMonkey
drawText( screen, ss.str().c_str(), 3, 23, 0, 0, 0 );


Yeah? :)

This topic is closed to new replies.

Advertisement