gpt4 book ai didi

c++ - SDL 瓦片 map 渲染错误 C++/C

转载 作者:行者123 更新时间:2023-11-28 07:07:42 30 4
gpt4 key购买 nike

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

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


map .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);
}


main.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 .你打电话Graphics::initScreenGame中的一个实例 game ,但稍后您尝试在 Graphics 中呈现Map 的实例实例 game.m ,其中 SDL 表面 screen未初始化。具体game.render();电话 game.m.renderMap(); ,它调用 game.m.g.drawImage(...) .

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

关于c++ - SDL 瓦片 map 渲染错误 C++/C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21517368/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com