// SDL GAME.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

#include <SDL.h>
#include <sdl_gdiplus.h>
#include <string>
#include <time.h>
#include <math.h>

#pragma comment(lib,"SDL")
#pragma comment(lib,"SDLmain")
#pragma comment(lib,"SDL_gdiplus")

#define MapWidth 500
#define view_wview 640
#define view_hview 480

SDL_Surface *screen;
bool keyLeft=0,
         keyRight=0,
         keyUp=0,
         keyDown=0,
     keyLeftPress=0,
         keyRightPress=0,
         keyUpPress=0,
         keyDownPress=0;
        
short mapPos[MapWidth][MapWidth]={0};
bool canPass[7]={0};
int view_xview=0,view_yview=0;

int random(int x){return rand()%(x+1);}
int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}

SDL_Surface* LoadIMG(std::string name){
        SDL_Surface* Loadedimage=NULL;
        SDL_Surface* Optimizedimage=NULL;
        Loadedimage=IMG_Load(name.c_str());

        if(Loadedimage!=NULL){
                Optimizedimage=SDL_DisplayFormat(Loadedimage);
                SDL_FreeSurface(Loadedimage);
                printf("\"%s\" loading success\n",name.c_str());
        }else printf("\"%s\" loading fail\n",name.c_str());

        return Optimizedimage;
}

typedef struct Position{
        int x,y;
}Position;

typedef struct Sprite{
        SDL_Surface *res;
        int width,height,xoffset,yoffset;
}Sprite;

typedef struct Object{
        Sprite spr;
        Position pos,targetPos;
        int flag;
}Object;

void SpriteInit(Sprite &spr){
        spr.width=1;
        spr.height=1;
        spr.xoffset=0;
        spr.yoffset=0;
        printf("width: %d / height: %d\n",spr.res->w,spr.res->h);
}

void ObjectInit(Object &obj,std::string res,int x,int y){
        obj.spr.res=LoadIMG(res);
        SpriteInit(obj.spr);
        obj.pos.x=x;
        obj.pos.y=y;
}

void DrawSurface(SDL_Surface *dst,Sprite spr,int index,int x,int y){
        SDL_Rect rectDst,rectSrc;
        rectDst.x=x-spr.xoffset;
        rectDst.y=y-spr.yoffset;
        rectSrc.x = (index%spr.width) * spr.res->w/spr.width;
        rectSrc.y = (index/spr.width) * spr.res->h/spr.height;
        rectSrc.w = spr.res->w/spr.width;
        rectSrc.h = spr.res->h/spr.height;
        SDL_BlitSurface(spr.res,&rectSrc,dst,&rectDst);
}

void Render(){
        SDL_Flip(screen);
}

void DrawObject(SDL_Surface *dst,Object &obj){
        DrawSurface(dst,obj.spr,0,obj.pos.x*16-view_xview,obj.pos.y*16-view_yview);
}

int main(int argc,char **argv){
        SDL_Init(SDL_INIT_VIDEO);

        screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE | SDL_DOUBLEBUF);

        //변수 선언
        bool quit=0;
        float fps=60;
        int startTick,i,j;

        SDL_Event event;

        //게임변수 선언
        int move_delay=0;
        short turn=0;

        //리소스 로드
        Sprite back;
        Sprite background;
        back.res=LoadIMG("res\\img\\back.png");
        SpriteInit(back);
        background.res=LoadIMG("res\\img\\background.png");
        SpriteInit(background);

        background.width=7;
        
        //게임관련 변수 초기화
        SDL_WM_SetCaption("SDL GAME TEST",NULL);
        srand((unsigned)time(NULL));
        for(i=0;i<MapWidth;i++)for(j=0;j<MapWidth;j++)
                mapPos[i][j]=random(6);

        //블록 통행여부
        canPass[0]=1;
        canPass[1]=1;
        canPass[2]=1;
        canPass[3]=1;
        canPass[4]=1;
        canPass[5]=1;
        canPass[6]=0;

        //인스턴스 생성        
        Object player;
        ObjectInit(player,"res\\img\\charactor.png",random(MapWidth),random(MapWidth));
        Object act[2000];
        for(i=0;i<2000;i++){
                ObjectInit(act[i],"res\\img\\charactor.png",random(MapWidth),random(MapWidth));
                act[i].flag=i<200?3:(1+random(1));
                act[i].targetPos.x=random(MapWidth);
                act[i].targetPos.y=random(MapWidth);
        }
        
        while(!quit){
                startTick=SDL_GetTicks();

                //프레임 초기화
                keyLeftPress=0;
                keyRightPress=0;
                keyUpPress=0;
                keyDownPress=0;

                if(SDL_PollEvent(&event)){
                        switch(event.type){
                        case SDL_QUIT:
                                quit=1;
                                break;
                        case SDL_KEYDOWN:
                                switch(event.key.keysym.sym){
                                case SDLK_LEFT:
                                        keyLeft=1;
                                        keyLeftPress=1;
                                        break;
                                case SDLK_RIGHT:
                                        keyRight=1;
                                        keyRightPress=1;
                                        break;
                                case SDLK_UP:
                                        keyUp=1;
                                        keyUpPress=1;
                                        break;
                                case SDLK_DOWN:
                                        keyDown=1;
                                        keyDownPress=1;
                                        break;
                                case SDLK_SPACE:
                                        for(j=-5;j<=5;j++){
                                                        for(i=-5;i<=5;i++){
                                                                if(i||j)printf("%d ",mapPos[player.pos.x+i][player.pos.y+j]);
                                                                else printf("  ");
                                                        }
                                                        printf("\n");
                                                }
                                        break;
                                }
                                break;
                        case SDL_KEYUP:
                                switch(event.key.keysym.sym){
                                case SDLK_LEFT:
                                        keyLeft=0;
                                        break;
                                case SDLK_RIGHT:
                                        keyRight=0;
                                        break;
                                case SDLK_UP:
                                        keyUp=0;
                                        break;
                                case SDLK_DOWN:
                                        keyDown=0;
                                        break;
                                }
                                break;
                        }
                }
                
                //캐릭터 이동
                if((keyRight||keyLeft||keyDown||keyUp)&&move_delay<=0){

                        move_delay=10;
                        if(keyRight&&canPass[mapPos[player.pos.x+1][player.pos.y]]){
                                player.pos.x+=1;
                                if(!keyLeft)turn++;
                        }
                        if(keyLeft&&canPass[mapPos[player.pos.x-1][player.pos.y]]){
                                player.pos.x-=1;
                                if(!keyRight)turn++;
                        }
                        if(keyDown&&canPass[mapPos[player.pos.x][player.pos.y+1]]){
                                player.pos.y+=1;
                                if(!keyUp)turn++;
                        }
                        if(keyUp&&canPass[mapPos[player.pos.x][player.pos.y-1]]){
                                player.pos.y-=1;
                                if(!keyDown)turn++;
                        }

                        player.pos.x=max(0,min(MapWidth-1,player.pos.x));
                        player.pos.y=max(0,min(MapWidth-1,player.pos.y));

                }else{move_delay-=1;}

                view_xview=player.pos.x*16-320;
                view_yview=player.pos.y*16-240;

                while(turn){
                        turn--;

                        for(i=0;i<2000;i++){
                                if(act[i].pos.x==act[i].targetPos.x&&act[i].pos.y==act[i].targetPos.y){
                                        act[i].targetPos.x=random(MapWidth);
                                        act[i].targetPos.y=random(MapWidth);
                                }

                                if((random(1)||act[i].pos.y==act[i].targetPos.y)&&act[i].pos.x!=act[i].targetPos.x){
                                        if(canPass[mapPos[act[i].pos.x+(act[i].pos.x<act[i].targetPos.x)-(act[i].pos.x>act[i].targetPos.x),act[i].pos.y]]){//여기서 오류
                                                act[i].pos.x+=(act[i].pos.x<act[i].targetPos.x)-(act[i].pos.x>act[i].targetPos.x);
                                        }
                                }else{
                                        if(canPass[mapPos[act[i].pos.x,act[i].pos.y+(act[i].pos.y<act[i].targetPos.y)-(act[i].pos.y>act[i].targetPos.y)]]){//여기서 오류
                                                act[i].pos.y+=(act[i].pos.y<act[i].targetPos.y)-(act[i].pos.y>act[i].targetPos.y);
                                        }
                                }
                        }
                }


                //Rendering
                DrawSurface(screen,back,0,0,0);
                for(j=max(0,-view_yview/16-1);j<min(30,-view_yview/16+MapWidth-1);j++)for(i=max(0,-view_xview/16-1);i<min(40,-view_xview/16+MapWidth-1);i++){
                        DrawSurface(screen,background,mapPos[view_xview/16+i][view_yview/16+j],i*16,j*16);
                }
                DrawObject(screen,player);
                for(i=0;i<2000;i++){
                        DrawObject(screen,act[i]);
                }
                Render();

                SDL_Delay((int)(1000/fps));
        }

        SDL_Quit();

        return 0;
}









실험해보니깐 mapPos 내부에 쓰레기값이 들어간거 같아... 전부 4502016 뭐 이런값들 줄줄 들어가있던데 무슨일이지.. 초기화도 다 시켰고,, ㅠ
SDL써서 만든거 ㅠ