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;
}
哦對!那真是愚蠢......我來自嵌入式C編程,前一段時間我學習了一些OOP,現在我正在學習C++和所有類類型的東西。但是,這是一個可怕的錯誤:D – Luca
我不贊同:)。儘管我仍然在學習C++的所有複雜性,並且確實需要很多東西。歡呼 – Meyer
非常感謝! ;) – Luca