Sign in to follow this  
Kampfschaf

C++ Questions (SDL)

Recommended Posts

Kampfschaf    122
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?

Share this post


Link to post
Share on other sites
someboddy    100
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...

Share this post


Link to post
Share on other sites
Kampfschaf    122
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);
}

Share this post


Link to post
Share on other sites
someboddy    100
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.

Share this post


Link to post
Share on other sites
rip-off    10979
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.

Share this post


Link to post
Share on other sites
Kampfschaf    122
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[i], 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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this