TrevorBradley
Active Member
- Joined
- Nov 6, 2007
- Messages
- 732
I'm making good progress on my SDL game, but I've hit a roadblock and could use some help.
What I'd like to be able to do is to clone an SDL surface, draw onto that clone, and SDL_Flip the surface onto the screen. The original surface is very complex and it would be best if I could render it once instead of with every frame displayed.
The problem is, I can't seem to clone an SDL Surface, or when I attempt to write to the clone it writes on the original surface.
Here's a bit of test code I've written to try to explain what I'm doing...
CODE
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include <stdio.h>
#include <stdlib.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_CENTER 300
#define SCREEN_DEPTH 16
int main(int argc, char *argv[]) {
SDL_Surface *screen1;
SDL_Surface *screen2;
SDL_Event event;
bool toggle=false;
SDL_Rect screen_rect;
screen_rect.x=0;
screen_rect.y=0;
screen_rect.h=SCREEN_HEIGHT;
screen_rect.w=SCREEN_WIDTH;
screen1 = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
filledCircleColor(screen1,SCREEN_CENTER,SCREEN_CENTER,200,0xFFFFFFFF);
SDL_Flip(screen1);
while(1) {
if( SDL_PollEvent( &event ) ) {
if( event.type == SDL_QUIT ) {
exit(0);
}
if ( event.type == SDL_KEYDOWN ) {
switch( event.key.keysym.sym ) {
case SDLK_SPACE:
toggle=!toggle;
break;
case SDLK_ESCAPE:
case SDLK_q:
exit(0);
break;
}
}
}
SDL_BlitSurface(screen1,&screen_rect,screen2,&screen_rect);
if (toggle)
filledCircleColor(screen2,SCREEN_CENTER,SCREEN_CENTER+100,100,0xFF0000FF);
else
filledCircleColor(screen2,SCREEN_CENTER,SCREEN_CENTER-100,100,0xFF0000FF);
SDL_Flip(screen2);
}
}
Here I want to preserve screen1 and leave it untouched. I want to copy screen1 to screen2, write on screen2, and then start over again, copying screen1 over screen2 again. But the code above appears to modify screen1 as well.
I thought there was some kind of surface alpha issue, but I have a feeling I'm doing something dumb...
Can anyone see what I'm doing wrong here? Thanks in advance!
What I'd like to be able to do is to clone an SDL surface, draw onto that clone, and SDL_Flip the surface onto the screen. The original surface is very complex and it would be best if I could render it once instead of with every frame displayed.
The problem is, I can't seem to clone an SDL Surface, or when I attempt to write to the clone it writes on the original surface.
Here's a bit of test code I've written to try to explain what I'm doing...
CODE
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include <stdio.h>
#include <stdlib.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_CENTER 300
#define SCREEN_DEPTH 16
int main(int argc, char *argv[]) {
SDL_Surface *screen1;
SDL_Surface *screen2;
SDL_Event event;
bool toggle=false;
SDL_Rect screen_rect;
screen_rect.x=0;
screen_rect.y=0;
screen_rect.h=SCREEN_HEIGHT;
screen_rect.w=SCREEN_WIDTH;
screen1 = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
filledCircleColor(screen1,SCREEN_CENTER,SCREEN_CENTER,200,0xFFFFFFFF);
SDL_Flip(screen1);
while(1) {
if( SDL_PollEvent( &event ) ) {
if( event.type == SDL_QUIT ) {
exit(0);
}
if ( event.type == SDL_KEYDOWN ) {
switch( event.key.keysym.sym ) {
case SDLK_SPACE:
toggle=!toggle;
break;
case SDLK_ESCAPE:
case SDLK_q:
exit(0);
break;
}
}
}
SDL_BlitSurface(screen1,&screen_rect,screen2,&screen_rect);
if (toggle)
filledCircleColor(screen2,SCREEN_CENTER,SCREEN_CENTER+100,100,0xFF0000FF);
else
filledCircleColor(screen2,SCREEN_CENTER,SCREEN_CENTER-100,100,0xFF0000FF);
SDL_Flip(screen2);
}
}
Here I want to preserve screen1 and leave it untouched. I want to copy screen1 to screen2, write on screen2, and then start over again, copying screen1 over screen2 again. But the code above appears to modify screen1 as well.
I thought there was some kind of surface alpha issue, but I have a feeling I'm doing something dumb...
Can anyone see what I'm doing wrong here? Thanks in advance!