title: SDL Tile Map Rendering Error C++/C
body: <p>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?</p>
  
  <h1>EDIT: I added the extra code that was missing.</h1>
  
  <p><br></p>
  
  <h1>Map.cpp</h1>
  
  <pre><code>#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 &lt; 10; y++)
      {
          for (int x = 0; x &lt; 10; x++)
          {
              int newPos = map[y][x];
              if (newPos == 0)
              {
                  g.drawImage(x * 32, y * 32, grassTile);
              }
          }
      }
  }
  </code></pre>
  
  <p><br></p>
  
  <h1>Graphics.cpp</h1>
  
  <pre><code>#include "Graphics.h"
  #include &lt;iostream&gt;
  #include &lt;SDL.h&gt;
  #include &lt;Windows.h&gt;
  using namespace std;
  Graphics::Graphics()
  : count( 0 )
  {
  
  }
  
  Graphics::~Graphics()
  {
  }
  
  int Graphics::getWidth(int i)
  {
      return this-&gt;width = i;
  }
  
  int Graphics::getHeight(int i)
  {
      return this-&gt;height = i;
  }
  
  void Graphics::initScreen(int width,int height, char* title)
  {
      if (!SDL_Init(SDL_INIT_EVERYTHING) == 1)
      {
          cout &lt;&lt; "SDL is running" &lt;&lt; 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 &lt; amountY; y++)
      {
          for (int x = 0; x &lt; 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-&gt;pixels != NULL)
          {
              Uint8* pixels = (Uint8*)screen-&gt;pixels;
              Uint8* indevidualPixel = pixels + (y * screen-&gt;pitch) + x;
              *indevidualPixel = SDL_MapRGB(screen-&gt;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,&amp;rect);
  }
  </code></pre>
  
  <p><br></p>
  
  <h1>Main.cpp></h1>
  
  <pre><code>#include &lt;SDL.h&gt;
  #include &lt;cstdio&gt;
  #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(&amp;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;
  };
  </code></pre>
  
  <p><br></p>
  
  <h1>Game.cpp</h1>
  
  <pre><code>#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 &lt; 10)
      {
          count++;
      }
  
      if (count == 10)
      {
          keyLogic();
          count = 0;
      }
  }
  void Game::render()
  {
      update();
      m.renderMap();
  
  }
  </code></pre>