2016-12-28 117 views
0

我正在使用SDL2來學習如何製作簡單的2D平臺。 我開始閱讀SDL遊戲開發和Lazy Foo的教程,並且我可以打開一個窗口並在窗口上繪製表面,並在窗口上繪製表面(如在傳統SDL中完成的那樣),但是如果嘗試使用Renderer函數進行硬件加速時也不會顯示任何東西! 我在Windows 10上運行,當我啓動應用程序時,它只顯示空的窗口。我試圖打印一些調試行,它似乎打開位圖,創建紋理,但是當我打印矩形的尺寸時,我用QueryTexture得到它返回0和0!SDL2 SLD_RenderCopy不顯示任何東西

//Game.cpp: 
#include <SDL.h> 
#include <SDL_image.h> 
#include "Game.h" 
#include <iostream> 

bool Game::init(const char *title,int x,int y,int width,int height,int flags) 
{ 
    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
     return false; 
    m_pWindow = SDL_CreateWindow(title,x,y,width,height,flags); 
    if (m_pWindow == NULL) 
     return false; 
    m_pRenderer = SDL_CreateRenderer(m_pWindow,-1,0); 
    if (m_pRenderer == NULL) 
     return false; 
    SDL_SetRenderDrawColor(m_pRenderer,0,0,0,255); 
    return true; 
} 

void Game::handle_events() 
{ 
    SDL_Event event; 
    while (SDL_PollEvent(&event) != 0) 
    { 
     switch (event.type) 
     { 
      case SDL_QUIT: 
       m_bRunning = false; 
       break; 
      default: 
       break; 
     } 
    } 
} 

void Game::render() 
{ 
    SDL_RenderClear(m_pRenderer); 
    SDL_RenderCopy(m_pRenderer,m_pTexture,&m_sourceRectangle,&m_destinationRectangle); 
    SDL_RenderPresent(m_pRenderer); 
} 

void Game::clean() 
{ 
    SDL_DestroyWindow(m_pWindow); 
    SDL_DestroyRenderer(m_pRenderer); 
    SDL_Quit(); 
} 

bool Game::loadImage() 
{ 
    SDL_Surface *surface = SDL_LoadBMP("logo.bmp"); 
    if (surface == NULL) 
     return false; 
    SDL_Texture *texture = SDL_CreateTextureFromSurface(m_pRenderer,surface); 
    if (texture == NULL) 
     return false; 
    SDL_FreeSurface(surface); 
    SDL_QueryTexture(m_pTexture,NULL,NULL,&m_sourceRectangle.w,&m_sourceRectangle.h); 
    m_destinationRectangle.x = m_sourceRectangle.x = 0; 
    m_destinationRectangle.y = m_sourceRectangle.y = 0; 
    m_destinationRectangle.w = m_sourceRectangle.w; 
    m_destinationRectangle.h = m_sourceRectangle.h; 
    return true; 
} 



// main.cpp: 
#include <SDL.h> 
#include "Game.h" 
#include <iostream> 
#include <Windows.h> 

Game *g_game = NULL; 

int main(int argc,char **argv) 
{ 
    AllocConsole(); 
    freopen("CON","w", stdout); 

    g_game = new Game(); 
    if (g_game->init("my 2D platform game!",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,800,600,SDL_WINDOW_SHOWN) != true) 
     return 1; 
    if (g_game->loadImage() != true) 
     return 2; 
    g_game->render(); 

    while (g_game->running()) 
    { 
     g_game->handle_events(); 
     g_game->update(); 
    } 

    g_game->clean(); 

    return 0; 
} 

回答

2

的問題是,你不 的Game::loadImage()功能正確分配質感。在線路

SDL_Texture *texture = SDL_CreateTextureFromSurface(m_pRenderer,surface); 

所創建的質感被分配到一個局部指針變量,它在函數的前端移到 超出範圍。相反,你需要assing 質感的類變量:

m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,surface); 

作爲一個說明,你是不是要 充分利用的C++功能,管理資源。我建議您在 上投入一些時間來了解推薦做法,例如從 C++ Core Guidelines

+0

哦對!那真是愚蠢......我來自嵌入式C編程,前一段時間我學習了一些OOP,現在我正在學習C++和所有類類型的東西。但是,這是一個可怕的錯誤:D – Luca

+0

我不贊同:)。儘管我仍然在學習C++的所有複雜性,並且確實需要很多東西。歡呼 – Meyer

+0

非常感謝! ;) – Luca