SDL 磁贴地图呈现错误 C++/c

SDL Tile Map Rendering Error C++/C

本文关键字:错误 C++ 地图 SDL      更新时间:2023-10-16

所以我正在使用 SDL 制作一个 2D 平台游戏。我有 C/C++ 方面的经验,但在过去的 8 个月里我并没有真正用它写过任何东西。我使用 SDL 渲染我的图形,我得到一个完全空白的屏幕,我似乎真的不知道为什么。谁能帮我弄清楚为什么这不是渲染?

编辑:我添加了缺少的额外代码。


地图.cpp

#include "Map.h"
int map[10][10] = { {0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
};
Map::Map()
:   g( Graphics() )
{
grassTile = SDL_LoadBMP("grass.bmp");
}
Map::~Map()
{
}
void Map::renderMap()
{
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 10; x++)
{
int newPos = map[y][x];
if (newPos == 0)
{
g.drawImage(x * 32, y * 32, grassTile);
}
}
}
}


图形.cpp

#include "Graphics.h"
#include <iostream>
#include <SDL.h>
#include <Windows.h>
using namespace std;
Graphics::Graphics()
: count( 0 )
{
}
Graphics::~Graphics()
{
}
int Graphics::getWidth(int i)
{
return this->width = i;
}
int Graphics::getHeight(int i)
{
return this->height = i;
}
void Graphics::initScreen(int width,int height, char* title)
{
if (!SDL_Init(SDL_INIT_EVERYTHING) == 1)
{
cout << "SDL is running" << endl;
}
SDL_WM_SetCaption(title,NULL);
screen = SDL_SetVideoMode(width,height,8,NULL);
getWidth(width);
getHeight(height);
}
void Graphics::beginFrame()
{
SDL_FillRect(screen,NULL,0x000000);
}
void Graphics::repaint()
{
SDL_Flip(screen);
}
void Graphics::loadImage(char* location, SDL_Surface* sur)
{
sur = SDL_LoadBMP(location);
}
void Graphics::renderTileMap(SDL_Surface* sur, int width, int height, int amountX, int amountY)
{
for (int y = 0; y < amountY; y++)
{
for (int x = 0; x < amountX; x++)
{
drawImage(x * width, y * height, sur);
}
}
}
void Graphics::pixel(int x,int y, int r,int g, int b)
{
if (screen != NULL)
{
if (screen->pixels != NULL)
{
Uint8* pixels = (Uint8*)screen->pixels;
Uint8* indevidualPixel = pixels + (y * screen->pitch) + x;
*indevidualPixel = SDL_MapRGB(screen->format,r,g,b);
}
}
}
void Graphics::drawImage(int x,int y, SDL_Surface* sur)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
SDL_BlitSurface(sur,NULL,screen,&rect);
}


主.cpp>

#include <SDL.h>
#include <cstdio>
#include "Graphics.h"
#include "Keyboard.h"
#include "Game.h"
SDL_Event event;
bool running = true;
Game game = Game();

int main(int argc, char* argv[])
{
game.k = Keyboard();
game.g.initScreen(900,500, "theDevCorner's SDL Tutorials!!!");
while (running)
{   
SDL_PollEvent(&event);
if (event.type == SDL_QUIT)
{
running = false;
}
if (event.type == SDL_KEYUP)
{
SDLKey keyReleased = event.key.keysym.sym;
if (keyReleased == SDLK_RIGHT)
{
game.k.right = false;
}
if (keyReleased == SDLK_LEFT)
{
game.k.left = false;
}
if (keyReleased == SDLK_DOWN)
{
game.k.down = false;
}
if (keyReleased == SDLK_UP)
{
game.k.up = false;
}
}
if (event.type == SDL_KEYDOWN)
{
SDLKey keyPressed = event.key.keysym.sym;
if (keyPressed == SDLK_RIGHT)
{
game.k.right = true;
}
if (keyPressed == SDLK_LEFT)
{
game.k.left = true;
}
if (keyPressed == SDLK_DOWN)
{
game.k.down = true;
}
if (keyPressed == SDLK_UP)
{
game.k.up = true;
}
}
game.g.beginFrame();
game.render();
game.g.repaint();
}
return 0;
};


游戏.cpp

#include "Game.h"
Game::Game()
: g(Graphics()),
x( 0 ),
y( 0 ),
m( Map() )
{
image = SDL_LoadBMP("grass.bmp");
}
Game::~Game()
{
}
void Game::keyLogic()
{
if (k.right)
{
x += 5;
}
if (k.left)
{
x -= 5;
}
if (k.down)
{
y += 5;
}
if (k.up)
{
y -= 5;
}
}
void Game::update()
{
if (count < 10)
{
count++;
}
if (count == 10)
{
keyLogic();
count = 0;
}
}
void Game::render()
{
update();
m.renderMap();
}

您有两个Graphics实例,一个在Game中,一个在Map中。您在Game实例game上调用Graphics::initScreen,但稍后您尝试在Map实例game.mGraphics实例中渲染,其中 SDL 表面screen未初始化。具体来说game.render();调用game.m.renderMap();,它调用game.m.g.drawImage(...)

确定Graphics实例的一个位置或使用引用。