lundi 29 février 2016

Tiled map Render Crashes. C++ & SDL2

I was trying to make program that renders basic tiled map really small only 6x8 and the tiles are 80x80. But when I built program and tried to launch it, it crashed at startup.

Here is my Source code:

#include <iostream>
#include <fstream>
#include <SDL.h>
#include <SDL_image.h>
#undef main
#pragma once

SDL_Window *window = NULL;
SDL_Renderer *renderTarget = NULL;

const int TILE_WIDTH = 80;
const int TILE_HEIGHT = 80;
const int TOTAL_TILES = 48;

const int LEVEL_WIDTH = 640;
const int LEVEL_HEIGHT = 480;


SDL_Texture *LoadTexture(std::string filePath, SDL_Renderer *renderTarget)
{
    SDL_Texture *texture = NULL;
    SDL_Surface *surface = IMG_Load(filePath.c_str());
    {
         texture = SDL_CreateTextureFromSurface(renderTarget, surface);
    }
    SDL_FreeSurface(surface);
    return texture;
}
class Tile
{
private:
    SDL_Texture *TileTexture;
    SDL_Rect TilePos;
    SDL_Rect Tile_TYPE;
public:
    Tile(int x, int y, int tileType);
    void Show(SDL_Renderer *renderTarget);
};
bool setTiles( Tile *tiles[] );

Tile::Tile(int x, int y, int tileType)
{
    TileTexture = LoadTexture("TileSheet.png", renderTarget);

    TilePos.x = x;
    TilePos.y = y;
    TilePos.w = TILE_WIDTH;
    TilePos.h = TILE_HEIGHT;

    Tile_TYPE.w = 80;
    Tile_TYPE.h = 80;

    if(tileType == 0)
    {
        Tile_TYPE.x = 0;
        Tile_TYPE.y = 80;
    }
    else if(tileType == 1)
    {
        Tile_TYPE.x = 0;
        Tile_TYPE.y = 0;
    }
}
void Tile::Show(SDL_Renderer *renderTarget)
{
    SDL_RenderCopy(renderTarget, TileTexture, &Tile_TYPE, &TilePos);
}
bool SetTiles( Tile *tiles [] )
{
    int x = 0, y = 0;

    std::ifstream map("Map.mcmp");

    {
        for(int i = 0; i < TOTAL_TILES; ++i )
        {
            int tileType = -1;

            map >> tileType;

            tiles[ i ] = new Tile( x, y, tileType );

            x += TILE_WIDTH;

            if( x >= LEVEL_WIDTH )
            {
                x = 0;
                y += TILE_HEIGHT;
            }
        }
    }
    map.close();
    return true;
}

int main(int argc, char *argv[])
{
    window = SDL_CreateWindow("tiledMap", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    renderTarget = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_Texture *PlayerTexture = LoadTexture("Player.png", renderTarget);

    SDL_Rect PlayerPos;

    Tile* tileSet[ TOTAL_TILES ];

    PlayerPos.x = 0;
    PlayerPos.y = 0;
    PlayerPos.w = 30;
    PlayerPos.h = 30;

    SDL_Event ev;

    bool isRunning = true;

    while(isRunning)
    {
        while(SDL_PollEvent(&ev) != 0)
        {
           if(ev.type == SDL_QUIT)
                isRunning = false;
            else if(ev.type == SDL_KEYDOWN)
            {
                switch(ev.key.keysym.sym)
                {
                case SDLK_DOWN:
                    PlayerPos.y++;
                case SDLK_UP:
                    PlayerPos.y--;
                case SDLK_RIGHT:
                    PlayerPos.x++;
                case SDLK_LEFT:
                    PlayerPos.x--;
                }
            }
        }
        SDL_RenderClear(renderTarget);

        for( int i = 0; i < TOTAL_TILES; ++i )
        {
            tileSet[ i ]->Show( renderTarget );
        }

        SDL_RenderCopy(renderTarget, PlayerTexture, NULL, &PlayerPos);
        SDL_RenderPresent(renderTarget);

    }
    SDL_DestroyTexture(PlayerTexture);
    SDL_DestroyRenderer(renderTarget);
    SDL_DestroyWindow(window);

    return 0;
}

-TileSheet.png is 80x160 sprite

Please tell me how to fix this. thank you.

Aucun commentaire:

Enregistrer un commentaire