devCorner
devCorner

Reputation: 193

SDL Tile Map Rendering Error C++/C

So I am making a 2D platformer using SDL. I have experience in C/C++, but I haven't really written anything with it in the past 8 months. I'm using SDL to render my graphics, and I get an entirely blank screen, and I can't really seem to figure out why. Can anybody help me figure out why this isn't rendering?

EDIT: I added the extra code that was missing.


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>

#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();

}

Upvotes: 0

Views: 694

Answers (1)

user3072164
user3072164

Reputation:

You have two instances of Graphics, one in Game and one in Map. You call Graphics::initScreen on the one in the Game instance game, but later you try to render in the Graphics instance of the Map instance game.m, where the SDL surface screen is uninitialized. Specifically game.render(); calls game.m.renderMap();, which calls game.m.g.drawImage(...).

Decide for one location of the Graphics instance or use references.

Upvotes: 1

Related Questions