我一直在围绕一些SDL2功能做一些非常简单的包装。我创建了一个包装SDL_Window*的类和一个包装SDL_Surface*的类。
在我的SDL_Surface包装器(SDL2::Surface)中,构造函数接受SDL2:::Window包装器并将SDL_Window* (通过getter调用)关联到SDL2::Surface中的SDL_Surface*成员变量。
然后,我有了SDL2::Surface::FillRect(),它调用:
SDL_FillRect(Surface, NULL, SDL_MapRGB(Surface->format, 0xFF, 0x00, 0xFF));在此构造下,我在调用SDL_FillRect时遇到访问冲突异常。但是,如果我将SDL_Window*和SDL_Surface都包装到一个包装类中,那么从该类对SDL_FillRect的调用就可以正常工作。
这两种方法之间可能有什么不同?
// Surface.h
namespace SDL2 {
class Window {
private:
SDL_Window* mWindow;
int ScreenWidth = 640;
int ScreenHeight = 480;
public:
Window();
~Window();
SDL_Window* GetWindow();
}
}
// Surface.cpp
SDL2::Window::Window() {
mWindow = SDL_CreateWindow("Breakout", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN);
}
SDL2::Window::~Window() {
SDL_DestroyWindow(mWindow);
{
// Surface.h
namespace SDL2 {
class Surface {
private:
SDL_Surface* Surface;
public:
Surface(SDL2::Window window);
~Surface();
void FillRect();
void Update(SDL2::Window window);
}
}
// Wrapper.cpp
SDL2::Surface::Surface(SDL2::Window window) {
Surface = SDL_GetWindowSurface(window.GetWindow());
}
void SDL2::Surface::FillRect() {
SDL_FillRect(Surface, NULL, SDL_MapRGB(Surface->format, 0xFF, 0x00, 0xFF)); // Access violation when this is called
}
void SDL2::Surface::Update(SDL2::Window window) {
SDL_UpdateWindowSurface(window.GetWindow());
}
// main.cpp
#include <SDL.h>
#include <stdio.h>
#include "Wrapper.h"
#include "Window.h"
int main() {
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return 1;
}
SDL2::Window window = SDL2::Window();
SDL2::Surface surface = SDL2::Surface(window);
surface.FillRect();
SDL_Delay(2000);
return 0;
}发布于 2018-08-16 03:14:58
Surface(SDL2::Window window)
^ missing &您通过值传递Windows,导致~Window()在Surface构造函数返回时立即对底层SDL_Window进行核化,从而使SDL_GetWindowSurface()返回的表面无效。
改为通过引用传递它们:
#include <SDL.h>
#include <stdio.h>
namespace SDL2
{
class Window
{
private:
SDL_Window* mWindow;
int ScreenWidth = 640;
int ScreenHeight = 480;
public:
Window()
{
mWindow = SDL_CreateWindow( "Breakout", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN );
}
~Window()
{
SDL_DestroyWindow( mWindow );
}
SDL_Window* GetWindow()
{
return mWindow;
}
};
class Surface
{
private:
SDL_Surface* mSurface;
public:
Surface( SDL2::Window& window )
{
mSurface = SDL_GetWindowSurface( window.GetWindow() );
}
void FillRect()
{
SDL_FillRect( mSurface, NULL, SDL_MapRGB( mSurface->format, 0xFF, 0x00, 0xFF ) );
}
void Update( SDL2::Window& window )
{
SDL_UpdateWindowSurface( window.GetWindow() );
}
};
}
int main( int argc, char** argv )
{
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
return 1;
}
SDL2::Window window = SDL2::Window();
SDL2::Surface surface = SDL2::Surface( window );
surface.FillRect();
surface.Update( window );
SDL_Delay( 2000 );
return 0;
}https://stackoverflow.com/questions/51854171
复制相似问题