SDL平铺图渲染错误C ++ / C.

时间:2014-02-02 22:55:46

标签: c++ rendering sdl

所以我正在使用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);
            }
        }
    }
}


Graphics.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的&GT;

#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;
};


Game.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();

}

1 个答案:

答案 0 :(得分:1)

您有两个Graphics个实例,一个位于Game,另一个位于Map。您在Graphics::initScreen实例Game中的game上调用了Graphics,但稍后您尝试在Map实例的game.m实例中进行呈现{{1} },SDL表面screen未初始化。具体来说,game.render();调用game.m.renderMap();,调用game.m.g.drawImage(...)

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