我试图让这个代码在不同的类上运行,它出现了一个空的 窗口,然后立即关闭。
的main.cpp
//The Headers
#include "SDL_SCREEN.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
//The event structure
SDL_Event event;
int main (int argc, char* args[]){
printf( "START" );
//The quit flag
bool quit = false;
//The Surface to be displayed
SDL_Surface* dis = NULL;
//The Screen Surface
SDL_Surface* screen = NULL;
//The screen
SDL_SCREEN screen_sdl;
if ( screen_sdl.init( "TEST", screen ) == false )
{
return 1;
}
dis = screen_sdl.load_image( "a.png" );
screen_sdl.apply_surface( 0, 0, dis, screen, NULL );
if ( SDL_Flip ( screen ) == -1 )
{
return 1;
}
while ( quit == false )
{
//While there are events to handle
while ( SDL_PollEvent( &event ) )
{
//if the user has xed out the window
if ( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
}
screen_sdl.clean_up( dis, screen );
SDL_Quit();
}
SDL_SCREEN.h
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
class SDL_SCREEN{
public:
SDL_Surface *load_image( char* );
void apply_surface( int, int, SDL_Surface*, SDL_Surface*, SDL_Rect* );
bool init(char*, SDL_Surface*);
void clean_up(SDL_Surface*, SDL_Surface*);
void flip_screen (SDL_Surface* screen){
if ( SDL_Flip ( screen ) == -1)
{
printf( "BLAH" );
}
}
};
SDL_SCREEN.cc
//The Headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL_SCREEN.h"
#include <stdio.h>
//Screen atrabutes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *SDL_SCREEN::load_image ( char *filename )
{
printf( "LOADING" );
//the image thats loaded
SDL_Surface* loadedImage = NULL;
//The optimized image
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename );
if ( loadedImage != NULL )
{
printf("Loaded");
//Create the optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if ( optimizedImage != NULL )
{
printf( " OPTIMIZED" );
//Color key the surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//return the optimized image
return optimizedImage;
}
void SDL_SCREEN::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//holds offsets
SDL_Rect offset;
//get offsets
offset.x = x;
offset.y = y;
//blit
SDL_BlitSurface( source, clip, destination, &offset );
}
void SDL_SCREEN::clean_up( SDL_Surface* to_clean, SDL_Surface* screen )
{
//Free the surfaces
SDL_FreeSurface( to_clean );
SDL_FreeSurface( screen );
}
bool SDL_SCREEN::init (char *Caption, SDL_Surface *screen)
{
//Initialize all SDL subsystems
if ( SDL_Init( SDL_INIT_EVERYTHING == -1 ) )
{
return false;
}
//set ut the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//if there was an error setting up the screen
if( screen == NULL )
{
return false;
}
//set up the window caption
SDL_WM_SetCaption( Caption, NULL );
return true;
}
答案 0 :(得分:1)
您的init
仅修改screen
参数,而不修改main中的变量
当你到达apply_surface
时,它仍然是NULL
(你可以使用调试器在几秒钟内找到它。)
您的SDL_Init
来电也有错位的括号 - 您使用SDL_INIT_EVERYTHING == -1
调用它而不是检查其返回值。
请改为:
SDL_Surface* SDL_SCREEN::init (char *Caption)
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return NULL;
}
SDL_Surface* screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if (screen != NULL)
{
SDL_WM_SetCaption( Caption, NULL );
}
return screen;
}
(旁注:由于SDL_SCREEN
没有任何状态,因此将它作为一个类是没有意义的。)
答案 1 :(得分:0)
尝试在SDL_Quit();
之前使用断点,并尝试在调试模式下运行它。
或者使用getch()强制程序在关闭之前显示输出。