├── README.md ├── SDL_collision_detection ├── JDFGUYIN.TTF ├── README.md ├── SDL2_ttf.dll ├── common.cpp ├── common.h ├── constant.h ├── dot.bmp ├── dot.cpp ├── dot.h ├── dot2.bmp ├── game.cpp ├── game.h ├── global.h ├── iconv.dll ├── libfreetype-6.dll ├── libpng16-16.dll ├── main.cpp ├── screenshot1.png ├── simhei.ttf ├── texturemanager.cpp └── texturemanager.h ├── SDL_role_walking ├── README.md ├── camera.cpp ├── camera.h ├── game.cpp ├── game.h ├── global.h ├── inputhandler.cpp ├── inputhandler.h ├── main.cpp ├── map.cpp ├── map.h ├── player.cpp ├── player.h ├── res │ ├── helloworld.png │ ├── town.png │ └── xiahouyi.png ├── screenshot.png └── vector2D.h ├── SDL_snake ├── AnimatedGraphic.cpp ├── AnimatedGraphic.h ├── Game.cpp ├── Game.h ├── GameObject.h ├── GameObjectFactory.cpp ├── GameObjectFactory.h ├── GameOverState.cpp ├── GameOverState.h ├── LoaderParams.h ├── Log.h ├── README.md ├── SDL2_ttf.dll ├── SDLGameObject.cpp ├── SDLGameObject.h ├── Snake.cpp ├── Snake.h ├── SoundManager.cpp ├── SoundManager.h ├── TextureManager.cpp ├── TextureManager.h ├── Vector2D.h ├── gamestate.h ├── gamestatemachine.cpp ├── gamestatemachine.h ├── inputhandler.cpp ├── inputhandler.h ├── main.cpp ├── mainmenustate.cpp ├── mainmenustate.h ├── menubutton.cpp ├── menubutton.h ├── menustate.h ├── pausestate.cpp ├── pausestate.h ├── playstate.cpp ├── playstate.h ├── res │ ├── backgroundmusic.wav │ ├── button.png │ ├── exit.png │ ├── gameover.mp3 │ ├── gameover.png │ ├── image.png │ ├── lazy.ttf │ ├── main.png │ ├── mouseclickbutton.wav │ ├── mousemovetobutton.wav │ ├── restart.png │ └── resume.png ├── screenshot1.png ├── screenshot2.png ├── screenshot3.png ├── stateparser.cpp ├── stateparser.h ├── test.xml ├── tinyxml │ ├── changes.txt │ ├── readme.txt │ ├── tinystr.cpp │ ├── tinystr.h │ ├── tinyxml.cpp │ ├── tinyxml.h │ ├── tinyxmlerror.cpp │ ├── tinyxmlparser.cpp │ ├── utf8test.gif │ ├── utf8test.xml │ ├── utf8testverify.xml │ └── xmltest.cpp └── zlib128-dll │ ├── DLL_FAQ.txt │ ├── README.txt │ ├── USAGE.txt │ ├── include │ ├── zconf.h │ └── zlib.h │ ├── lib │ ├── zdll.lib │ └── zlib.def │ ├── test │ ├── example_d.exe │ └── minigzip_d.exe │ └── zlib1.dll ├── SDL_video_player ├── README.md ├── common.cpp ├── common.h ├── main.cpp └── screenshot │ ├── 1.png │ └── 2.png ├── screenshot ├── 1.png ├── 2.png └── 3.jpg ├── 《SFML Essentials》 ├── Animating Sprites │ ├── Animator │ │ ├── Animator.cpp │ │ ├── Animator.h │ │ ├── AssetManager.cpp │ │ ├── AssetManager.h │ │ ├── main.cpp │ │ ├── myTexture.png │ │ └── spritesheet.png │ └── test animation │ │ ├── AssetManager.cpp │ │ ├── AssetManager.h │ │ └── main.cpp ├── Assets │ ├── crystal.png │ ├── cube.png │ ├── cube_tiled.png │ ├── egg.png │ └── leaf.png ├── Helloworld │ └── main.cpp ├── Loading and Using Textures │ ├── AssetManagement │ │ ├── AssetManager.cpp │ │ ├── AssetManager.h │ │ ├── main.cpp │ │ ├── myTexture1.png │ │ └── myTexture2.png │ ├── cube.cpp │ └── cube.png └── Simple Game │ └── main.cpp └── 《SFML Game Development》 ├── Chapter1 ├── game.cpp ├── game.h ├── main.cpp └── res │ └── Eagle.png └── Chapter2 ├── ResourceHolder.hpp ├── ResourceHolder.inl ├── main.cpp └── res ├── Desert.png └── Eagle.png /README.md: -------------------------------------------------------------------------------- 1 | ## SFML经典项目 2 | #### SFML制作的2d rpg游戏:Cendrics2 测试截图(源码: https://github.com/tizian/Cendric2) 3 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/screenshot/1.png) 4 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/screenshot/2.png) 5 | 6 | 7 | ## SDL经典项目 8 | #### SDL复刻版仙剑奇侠传:sdlpal 测试截图(源码:https://github.com/sdlpal/sdlpal,使用SDL1.2开发) 9 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/screenshot/3.jpg) 10 | 11 | #### 用SDL和FFMpeg库实现的游戏动画播放器: 12 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_video_player/screenshot/1.png) 13 | 14 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_video_player/screenshot/2.png) 15 | 16 | 17 | ## 自己用SDL写的小程序 18 | #### SDL事件处理与绘图功能实现的角色行走: 19 | 20 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_role_walking/screenshot.png) 21 | 22 | #### SDL实现的极简贪吃蛇游戏: 23 | 24 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_snake/screenshot1.png) 25 | 26 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_snake/screenshot2.png) 27 | 28 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_snake/screenshot3.png) 29 | -------------------------------------------------------------------------------- /SDL_collision_detection/JDFGUYIN.TTF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/JDFGUYIN.TTF -------------------------------------------------------------------------------- /SDL_collision_detection/README.md: -------------------------------------------------------------------------------- 1 | ### 1、说明 2 | 一个支持SDL输出中文,外加碰撞检测的小例子。 3 | 4 | ### 2、注意事项 5 | - 链接器-输入-附加依赖项要包含SDL2相关的lib文件: 6 | - SDL2.lib 7 | - SDL2main.lib 8 | - SDL2_image.lib 9 | - SDL2_mixer.lib 10 | - SDL2_ttf.lib 11 | - 最后要把SDL2相关的dll文件放在Windows系统目录或exe同一目录下! 12 | 13 | 14 | ### 3、效果演示 15 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_collision_detection/screenshot1.png) 16 | 17 | 18 | -------------------------------------------------------------------------------- /SDL_collision_detection/SDL2_ttf.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/SDL2_ttf.dll -------------------------------------------------------------------------------- /SDL_collision_detection/common.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/common.cpp -------------------------------------------------------------------------------- /SDL_collision_detection/common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | std::vector getUnicode(const std::string& str); -------------------------------------------------------------------------------- /SDL_collision_detection/constant.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | //Screen dimension constants 4 | const int SCREEN_WIDTH = 640; 5 | const int SCREEN_HEIGHT = 480; -------------------------------------------------------------------------------- /SDL_collision_detection/dot.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/dot.bmp -------------------------------------------------------------------------------- /SDL_collision_detection/dot.cpp: -------------------------------------------------------------------------------- 1 | #include "dot.h" 2 | #include "constant.h" //for SCREEN_HEIGHT, SCREEN_WIDTH 3 | #include "texturemanager.h" //for gDotTexture 4 | 5 | CDot::CDot() 6 | { 7 | //Initialize the offsets 8 | mPosX = 0; 9 | mPosY = 0; 10 | 11 | //Set collision box dimension 12 | mCollider.w = DOT_WIDTH; 13 | mCollider.h = DOT_HEIGHT; 14 | 15 | //Initialize the velocity 16 | mVelX = 0; 17 | mVelY = 0; 18 | } 19 | 20 | 21 | CDot::~CDot() 22 | { 23 | } 24 | 25 | void CDot::handleEvent(SDL_Event& e) 26 | { 27 | //If a key was pressed 28 | if (e.type == SDL_KEYDOWN && e.key.repeat == 0) 29 | { 30 | //Adjust the velocity 31 | switch (e.key.keysym.sym) 32 | { 33 | case SDLK_UP: mVelY -= DOT_VEL; break; 34 | case SDLK_DOWN: mVelY += DOT_VEL; break; 35 | case SDLK_LEFT: mVelX -= DOT_VEL; break; 36 | case SDLK_RIGHT: mVelX += DOT_VEL; break; 37 | } 38 | } 39 | //If a key was released 40 | else if (e.type == SDL_KEYUP && e.key.repeat == 0) 41 | { 42 | //Adjust the velocity 43 | switch (e.key.keysym.sym) 44 | { 45 | case SDLK_UP: mVelY += DOT_VEL; break; 46 | case SDLK_DOWN: mVelY -= DOT_VEL; break; 47 | case SDLK_LEFT: mVelX += DOT_VEL; break; 48 | case SDLK_RIGHT: mVelX -= DOT_VEL; break; 49 | } 50 | } 51 | } 52 | 53 | void CDot::move(SDL_Rect& wall) 54 | { 55 | //Move the dot left or right 56 | mPosX += mVelX; 57 | mCollider.x = mPosX; 58 | 59 | //If the dot collided or went too far to the left or right 60 | if ((mPosX < 0) || (mPosX + DOT_WIDTH > SCREEN_WIDTH) || checkCollision(mCollider, wall)) 61 | { 62 | //Move back 63 | mPosX -= mVelX; 64 | mCollider.x = mPosX; 65 | } 66 | 67 | //Move the dot up or down 68 | mPosY += mVelY; 69 | mCollider.y = mPosY; 70 | 71 | //If the dot collided or went too far up or down 72 | if ((mPosY < 0) || (mPosY + DOT_HEIGHT > SCREEN_HEIGHT) || checkCollision(mCollider, wall)) 73 | { 74 | //Move back 75 | mPosY -= mVelY; 76 | mCollider.y = mPosY; 77 | } 78 | } 79 | 80 | void CDot::render() 81 | { 82 | //Show the dot 83 | gDotTexture.render(mPosX, mPosY); 84 | } 85 | 86 | bool checkCollision(SDL_Rect a, SDL_Rect b) 87 | { 88 | //The sides of the rectangles 89 | int leftA, leftB; 90 | int rightA, rightB; 91 | int topA, topB; 92 | int bottomA, bottomB; 93 | 94 | //Calculate the sides of rect A 95 | leftA = a.x; 96 | rightA = a.x + a.w; 97 | topA = a.y; 98 | bottomA = a.y + a.h; 99 | 100 | //Calculate the sides of rect B 101 | leftB = b.x; 102 | rightB = b.x + b.w; 103 | topB = b.y; 104 | bottomB = b.y + b.h; 105 | 106 | //If any of the sides from A are outside of B 107 | if (bottomA <= topB) 108 | { 109 | return false; 110 | } 111 | 112 | if (topA >= bottomB) 113 | { 114 | return false; 115 | } 116 | 117 | if (rightA <= leftB) 118 | { 119 | return false; 120 | } 121 | 122 | if (leftA >= rightB) 123 | { 124 | return false; 125 | } 126 | 127 | //If none of the sides from A are outside B 128 | return true; 129 | } -------------------------------------------------------------------------------- /SDL_collision_detection/dot.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | //The dot that will move around on the screen 6 | class CDot 7 | { 8 | public: 9 | 10 | //Initializes the variables 11 | CDot(); 12 | ~CDot(); 13 | 14 | //The dimensions of the dot 15 | static const int DOT_WIDTH = 20; 16 | static const int DOT_HEIGHT = 20; 17 | 18 | //Maximum axis velocity of the dot 19 | static const int DOT_VEL = 5; 20 | 21 | //Takes key presses and adjusts the dot's velocity 22 | void handleEvent(SDL_Event& e); 23 | 24 | //Moves the dot and checks collision 25 | void move(SDL_Rect& wall); 26 | 27 | //Shows the dot on the screen 28 | void render(); 29 | 30 | private: 31 | //The X and Y offsets of the dot 32 | int mPosX, mPosY; 33 | 34 | //The velocity of the dot 35 | int mVelX, mVelY; 36 | 37 | //Dot's collision box 38 | SDL_Rect mCollider; 39 | }; 40 | 41 | bool checkCollision(SDL_Rect a, SDL_Rect b); -------------------------------------------------------------------------------- /SDL_collision_detection/dot2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/dot2.bmp -------------------------------------------------------------------------------- /SDL_collision_detection/game.cpp: -------------------------------------------------------------------------------- 1 | #include "game.h" 2 | -------------------------------------------------------------------------------- /SDL_collision_detection/game.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/game.h -------------------------------------------------------------------------------- /SDL_collision_detection/global.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include //clw modify 20190122 5 | 6 | //The window we'll be rendering to 7 | extern SDL_Window *g_pWindow; 8 | 9 | //The window renderer 10 | extern SDL_Renderer *g_pRenderer; 11 | 12 | //Globally used font 13 | extern TTF_Font *g_pFont; //clw modify 20190122 14 | 15 | -------------------------------------------------------------------------------- /SDL_collision_detection/iconv.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/iconv.dll -------------------------------------------------------------------------------- /SDL_collision_detection/libfreetype-6.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/libfreetype-6.dll -------------------------------------------------------------------------------- /SDL_collision_detection/libpng16-16.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/libpng16-16.dll -------------------------------------------------------------------------------- /SDL_collision_detection/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/main.cpp -------------------------------------------------------------------------------- /SDL_collision_detection/screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/screenshot1.png -------------------------------------------------------------------------------- /SDL_collision_detection/simhei.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/simhei.ttf -------------------------------------------------------------------------------- /SDL_collision_detection/texturemanager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_collision_detection/texturemanager.cpp -------------------------------------------------------------------------------- /SDL_collision_detection/texturemanager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //Texture wrapper class 9 | class CTextureManager 10 | { 11 | public: 12 | //Initializes variables 13 | CTextureManager(); 14 | 15 | //Deallocates memory 16 | ~CTextureManager(); 17 | 18 | //Loads image at specified path 19 | bool loadFromFile(std::string path); 20 | 21 | //Creates image from font string 22 | bool loadFromRenderedText(const std::string& textureText, SDL_Color textColor); 23 | 24 | //Deallocates texture 25 | void free(); 26 | 27 | //Set color modulation 28 | void setColor(Uint8 red, Uint8 green, Uint8 blue); 29 | 30 | //Set blending 31 | void setBlendMode(SDL_BlendMode blending); 32 | 33 | //Set alpha modulation 34 | void setAlpha(Uint8 alpha); 35 | 36 | //Renders texture at given point 37 | void render(int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE); 38 | 39 | //Gets image dimensions 40 | int getWidth(); 41 | int getHeight(); 42 | 43 | private: 44 | //The actual hardware texture 45 | SDL_Texture* mTexture; 46 | 47 | //Image dimensions 48 | int mWidth; 49 | int mHeight; 50 | }; 51 | 52 | //Rendered texture 53 | extern CTextureManager gDotTexture; //Scene textures 54 | extern CTextureManager gTextTexture; //clw modify 20190122 55 | -------------------------------------------------------------------------------- /SDL_role_walking/README.md: -------------------------------------------------------------------------------- 1 | ### 1、说明 2 | 利用SDL事件处理与绘图功能实现**角色行走**的一个小例子,按下shift可以跑动。我自己使用Mov Gear软件做的Sprite Sheet,里面包含了八个方向的静止、行走和跑动图。 3 | 4 | ### 2、效果演示 5 | 这里使用到了仙剑2的城镇场景和幽城幻剑录的主要角色夏侯仪。测试了一下人物在地图行走的效果,除了地图没加遮挡层,然后人物在45°方向行走之后停下来的朝向有点问题,其他都还算OK。 6 | 7 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_role_walking/screenshot.png) 8 | 9 | 10 | -------------------------------------------------------------------------------- /SDL_role_walking/camera.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/camera.cpp -------------------------------------------------------------------------------- /SDL_role_walking/camera.h: -------------------------------------------------------------------------------- 1 | #ifndef __CAMERA_H__ 2 | #define __CAMERA_H__ 3 | 4 | #include 5 | 6 | class CCamera 7 | { 8 | public: 9 | static CCamera* Instance() 10 | { 11 | if (s_pInstance == NULL) 12 | s_pInstance = new(CCamera); 13 | return s_pInstance; 14 | } 15 | 16 | void Update(); 17 | 18 | void SetX(int x) { m_x = x; } 19 | void SetY(int y) { m_y = y; } 20 | int GetX() { return m_x; } 21 | int GetY() { return m_y; } 22 | 23 | private: 24 | CCamera(); 25 | ~CCamera(); 26 | 27 | static CCamera* s_pInstance; 28 | int m_x = 0; 29 | int m_y = 0; 30 | }; 31 | 32 | #endif /* defined(__CAMERA_H__)*/ -------------------------------------------------------------------------------- /SDL_role_walking/game.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/game.cpp -------------------------------------------------------------------------------- /SDL_role_walking/game.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/game.h -------------------------------------------------------------------------------- /SDL_role_walking/global.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/global.h -------------------------------------------------------------------------------- /SDL_role_walking/inputhandler.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/inputhandler.cpp -------------------------------------------------------------------------------- /SDL_role_walking/inputhandler.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLW_INPUTHANDLER_H__ 2 | #define __CLW_INPUTHANDLER_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include "vector2D.h" 8 | 9 | enum mouse_buttons 10 | { 11 | LEFT = 0, 12 | MIDDLE = 1, 13 | RIGHT = 2 14 | }; 15 | 16 | class CInputHandler 17 | { 18 | public: 19 | 20 | static CInputHandler* Instance() 21 | { 22 | if(s_pInstance == 0) 23 | { 24 | s_pInstance = new CInputHandler(); 25 | } 26 | 27 | return s_pInstance; 28 | } 29 | 30 | // init joysticks 31 | void initialiseJoysticks(); 32 | bool joysticksInitialised() const { return m_bJoysticksInitialised; } 33 | 34 | void reset(); 35 | 36 | // update and clean the input handler 37 | void update(); 38 | void clean(); 39 | 40 | // keyboard events 41 | bool isKeyDown(SDL_Scancode key) const; 42 | 43 | // joystick events 44 | int getAxisX(int joy, int stick) const; 45 | int getAxisY(int joy, int stick) const; 46 | bool getButtonState(int joy, int buttonNumber) const; 47 | 48 | // mouse events 49 | bool getMouseButtonState(int buttonNumber) const; 50 | Vector2D* getMousePosition() const; 51 | 52 | private: 53 | 54 | CInputHandler(); 55 | ~CInputHandler(); 56 | 57 | CInputHandler(const CInputHandler&); 58 | CInputHandler& operator=(const CInputHandler&); 59 | 60 | // private functions to handle different event types 61 | 62 | // handle keyboard events 63 | void onKeyDown(); 64 | void onKeyUp(); 65 | 66 | // handle mouse events 67 | void onMouseMove(SDL_Event& event); 68 | void onMouseButtonDown(SDL_Event& event); 69 | void onMouseButtonUp(SDL_Event& event); 70 | 71 | // handle joysticks events 72 | void onJoystickAxisMove(SDL_Event& event); 73 | void onJoystickButtonDown(SDL_Event& event); 74 | void onJoystickButtonUp(SDL_Event& event); 75 | 76 | // member variables 77 | 78 | // keyboard specific 79 | const Uint8* m_keystates; 80 | 81 | // joystick specific 82 | std::vector> m_joystickValues; 83 | std::vector m_joysticks; 84 | std::vector> m_buttonStates; 85 | bool m_bJoysticksInitialised; 86 | static const int m_joystickDeadZone = 10000; 87 | 88 | // mouse specific 89 | std::vector m_mouseButtonStates; 90 | Vector2D* m_mousePosition; 91 | 92 | // singleton 93 | static CInputHandler* s_pInstance; 94 | }; 95 | 96 | #endif /* defined(__InputHandler_H__) */ 97 | -------------------------------------------------------------------------------- /SDL_role_walking/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/main.cpp -------------------------------------------------------------------------------- /SDL_role_walking/map.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/map.cpp -------------------------------------------------------------------------------- /SDL_role_walking/map.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/map.h -------------------------------------------------------------------------------- /SDL_role_walking/player.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/player.cpp -------------------------------------------------------------------------------- /SDL_role_walking/player.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/player.h -------------------------------------------------------------------------------- /SDL_role_walking/res/helloworld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/res/helloworld.png -------------------------------------------------------------------------------- /SDL_role_walking/res/town.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/res/town.png -------------------------------------------------------------------------------- /SDL_role_walking/res/xiahouyi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/res/xiahouyi.png -------------------------------------------------------------------------------- /SDL_role_walking/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_role_walking/screenshot.png -------------------------------------------------------------------------------- /SDL_role_walking/vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector2D_H__ 2 | #define __Vector2D_H__ 3 | 4 | #include 5 | #include 6 | 7 | class Vector2D 8 | { 9 | public: 10 | Vector2D() 11 | { 12 | m_x = 0; 13 | m_y = 0; 14 | } 15 | 16 | Vector2D(int x, int y): m_x(x), m_y(y) {} 17 | 18 | const int GetX() { return m_x; } 19 | const int GetY() { return m_y; } 20 | 21 | void SetX(int x) { m_x = x; } 22 | void SetY(int y) { m_y = y; } 23 | 24 | int Length() 25 | { 26 | return (int)sqrt(m_x * m_x + m_y * m_y); 27 | } 28 | 29 | Vector2D operator+(const Vector2D& v2) const 30 | { 31 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 32 | } 33 | 34 | friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2) 35 | { 36 | v1.m_x += v2.m_x; 37 | v1.m_y += v2.m_y; 38 | 39 | return v1; 40 | } 41 | 42 | Vector2D operator-(const Vector2D& v2) const 43 | { 44 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 45 | } 46 | 47 | friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2) 48 | { 49 | v1.m_x -= v2.m_x; 50 | v1.m_y -= v2.m_y; 51 | 52 | return v1; 53 | } 54 | 55 | Vector2D operator*(int scalar) 56 | { 57 | return Vector2D(m_x * scalar, m_y * scalar); 58 | } 59 | 60 | Vector2D& operator*=(int scalar) 61 | { 62 | m_x *= scalar; 63 | m_y *= scalar; 64 | 65 | return *this; 66 | } 67 | 68 | Vector2D operator/(int scalar) 69 | { 70 | return Vector2D(m_x / scalar, m_y / scalar); 71 | } 72 | 73 | Vector2D& operator/=(int scalar) 74 | { 75 | m_x /= scalar; 76 | m_y /= scalar; 77 | 78 | return *this; 79 | } 80 | 81 | 82 | void Normalize() 83 | { 84 | int l = Length(); 85 | if ( l > 0) 86 | { 87 | (*this) *= 1 / l; 88 | } 89 | } 90 | 91 | private: 92 | int m_x; 93 | int m_y; 94 | }; 95 | 96 | #endif /* defined(__Vector2D_H__) */ 97 | -------------------------------------------------------------------------------- /SDL_snake/AnimatedGraphic.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // AnimatedGraphic.cpp 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 17/02/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #include "AnimatedGraphic.h" 10 | 11 | using namespace std; 12 | 13 | AnimatedGraphic::AnimatedGraphic() : SDLGameObject() 14 | { 15 | 16 | } 17 | 18 | void AnimatedGraphic::load(std::unique_ptr const &pParams) 19 | { 20 | SDLGameObject::load(pParams); 21 | m_animSpeed = pParams->getAnimSpeed(); 22 | } 23 | 24 | void AnimatedGraphic::draw() 25 | { 26 | SDLGameObject::draw(); 27 | } 28 | 29 | void AnimatedGraphic::update() 30 | { 31 | m_currentFrame = int(((SDL_GetTicks() / (1000 / m_animSpeed)) % m_numFrames)); 32 | } 33 | 34 | void AnimatedGraphic::clean() 35 | { 36 | SDLGameObject::clean(); 37 | } -------------------------------------------------------------------------------- /SDL_snake/AnimatedGraphic.h: -------------------------------------------------------------------------------- 1 | // 2 | // AnimatedGraphic.h 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 17/02/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #ifndef __SDL_Game_Programming_Book__AnimatedGraphic__ 10 | #define __SDL_Game_Programming_Book__AnimatedGraphic__ 11 | 12 | #include 13 | #include "GameObjectFactory.h" 14 | #include "SDLGameObject.h" 15 | 16 | class AnimatedGraphic : public SDLGameObject 17 | { 18 | public: 19 | 20 | AnimatedGraphic(); 21 | virtual ~AnimatedGraphic() {} 22 | 23 | virtual void load(std::unique_ptr const &pParams); 24 | 25 | virtual void draw(); 26 | virtual void update(); 27 | virtual void clean(); 28 | 29 | private: 30 | 31 | int m_animSpeed; 32 | int m_frameCount; 33 | }; 34 | 35 | class AnimatedGraphicCreator : public BaseCreator 36 | { 37 | public: 38 | 39 | virtual GameObject* createGameObject() const 40 | { 41 | return new AnimatedGraphic(); 42 | } 43 | }; 44 | 45 | #endif /* defined(__SDL_Game_Programming_Book__AnimatedGraphic__) */ 46 | -------------------------------------------------------------------------------- /SDL_snake/Game.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/Game.cpp -------------------------------------------------------------------------------- /SDL_snake/Game.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/Game.h -------------------------------------------------------------------------------- /SDL_snake/GameObject.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/GameObject.h -------------------------------------------------------------------------------- /SDL_snake/GameObjectFactory.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GameObjectFactory.cpp 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 26/02/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #include "GameObjectFactory.h" 10 | 11 | GameObjectFactory* GameObjectFactory::pInstance = 0; 12 | -------------------------------------------------------------------------------- /SDL_snake/GameObjectFactory.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameObjectFactory.h 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 24/02/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #ifndef __GAMEOBJECTFACTORY_H__ 10 | #define __GAMEOBJECTFACTORY_H__ 11 | 12 | #include 13 | #include 14 | #include 15 | #include "GameObject.h" 16 | 17 | class BaseCreator 18 | { 19 | public: 20 | 21 | virtual GameObject* createGameObject() const = 0; 22 | virtual ~BaseCreator() {} 23 | }; 24 | 25 | class GameObjectFactory 26 | { 27 | public: 28 | 29 | static GameObjectFactory* Instance() 30 | { 31 | if(pInstance == 0) 32 | { 33 | pInstance = new GameObjectFactory(); 34 | } 35 | 36 | return pInstance; 37 | } 38 | 39 | bool registerType(std::string typeID, BaseCreator* pCreator) 40 | { 41 | std::map::iterator it = m_creators.find(typeID); 42 | 43 | // if the type is already registered, do nothing 44 | if(it != m_creators.end()) 45 | { 46 | delete pCreator; 47 | } 48 | 49 | m_creators[typeID] = pCreator; 50 | 51 | return true; 52 | } 53 | 54 | GameObject* create(std::string typeID) 55 | { 56 | std::map::iterator it = m_creators.find(typeID); 57 | 58 | if(it == m_creators.end()) 59 | { 60 | std::cout << "could not find type: " << typeID << "\n"; 61 | return NULL; 62 | } 63 | 64 | BaseCreator* pCreator = (*it).second; 65 | return pCreator->createGameObject(); 66 | } 67 | 68 | private: 69 | 70 | GameObjectFactory() {} 71 | ~GameObjectFactory() {} 72 | 73 | std::map m_creators; 74 | 75 | static GameObjectFactory* pInstance; 76 | }; 77 | 78 | typedef GameObjectFactory TheGameObjectFactory; 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /SDL_snake/GameOverState.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/GameOverState.cpp -------------------------------------------------------------------------------- /SDL_snake/GameOverState.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameOverState.h 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 17/02/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #ifndef __SDL_Game_Programming_Book__GameOverState__ 10 | #define __SDL_Game_Programming_Book__GameOverState__ 11 | 12 | #include 13 | #include 14 | #include "MenuState.h" 15 | 16 | using namespace std; 17 | 18 | class GameObject; 19 | 20 | class GameOverState : public MenuState 21 | { 22 | public: 23 | 24 | virtual ~GameOverState() {} 25 | 26 | virtual void update(); 27 | virtual void render(); 28 | 29 | virtual bool onEnter(); 30 | virtual bool onExit(); 31 | 32 | virtual std::string getStateID() const { return s_gameOverID; } 33 | 34 | virtual void setCallbacks(const std::vector& callbacks); 35 | 36 | private: 37 | 38 | static void s_gameOverToMain(); 39 | static void s_restartPlay(); 40 | 41 | static const std::string s_gameOverID; 42 | 43 | vector m_gameObjects; 44 | }; 45 | 46 | #endif /* defined(__SDL_Game_Programming_Book__GameOverState__) */ 47 | -------------------------------------------------------------------------------- /SDL_snake/LoaderParams.h: -------------------------------------------------------------------------------- 1 | // 2 | // LoaderParams.h 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 19/01/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #ifndef SDL_Game_Programming_Book_LoaderParams_h 10 | #define SDL_Game_Programming_Book_LoaderParams_h 11 | 12 | #include 13 | 14 | class LoaderParams 15 | { 16 | public: 17 | 18 | LoaderParams(int x, int y, int width, int height, std::string textureID, int numFrames, 19 | int callbackID = 0, int animSpeed = 0) : 20 | m_x(x), 21 | m_y(y), 22 | m_width(width), 23 | m_height(height), 24 | m_textureID(textureID), 25 | m_numFrames(numFrames), 26 | m_callbackID(callbackID), 27 | m_animSpeed(animSpeed) 28 | { 29 | 30 | } 31 | 32 | int getX() const { return m_x; } 33 | int getY() const { return m_y; } 34 | int getWidth() const { return m_width; } 35 | int getHeight() const { return m_height; } 36 | std::string getTextureID() const { return m_textureID; } 37 | int getNumFrames() const { return m_numFrames; } 38 | int getCallbackID() const { return m_callbackID; } 39 | int getAnimSpeed() const { return m_animSpeed; } 40 | 41 | private: 42 | 43 | int m_x; 44 | int m_y; 45 | 46 | int m_width; 47 | int m_height; 48 | 49 | int m_callbackID; 50 | int m_animSpeed; 51 | 52 | std::string m_textureID; 53 | 54 | int m_numFrames; 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /SDL_snake/Log.h: -------------------------------------------------------------------------------- 1 | //============================================================================== 2 | /* 3 | Basic macro for logging (can be extended for other target builds; i.e, using 4 | NSLog for OS X / iOS). Could also be modified to log to a file instead of 5 | console. 6 | 7 | 3/11/2014 8 | SDLTutorials.com 9 | Tim Jones 10 | */ 11 | //============================================================================== 12 | #ifndef __LOG_H__ 13 | #define __LOG_H__ 14 | 15 | #include 16 | 17 | #define DEBUG 1 18 | 19 | #ifdef DEBUG 20 | #define Log(...) printf(__VA_ARGS__); printf("\n"); 21 | #else 22 | #define Log(...) ; 23 | #endif 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /SDL_snake/README.md: -------------------------------------------------------------------------------- 1 | ## A game called 'gluttonous snake' made by SDL2, easy to expand. 2 | 3 | ### 1、说明 4 | 贪吃蛇游戏,也是一个简单的游戏框架,主要功能包括: 5 | - 多种游戏状态之间的转换,包括游戏开始状态、游戏进行状态、暂停状态以及游戏结束状态; 6 | - 控制物体匀速或加速移动,碰撞检测,随机生成以及分数显示; 7 | - 鼠标选中、点击按钮时不同的特效与音效,游戏进行时播放背景音乐; 8 | - 支持源码中修改窗口大小同时保持从.xml读取的object可以根据窗口大小等比例缩放。 9 | 10 | 11 | ### 2、注意事项 12 | - 新建VS2017项目,把snake文件夹内的.cpp和.h文件拖进来(不包括子目录内的文件); 13 | - 配置项目属性-VC++目录-包含目录添加tinyxml文件夹(另外还需要SDL2相关的头文件); 14 | - tinyxml文件夹内的四个tinyxml相关的.cpp文件拖到VS2017项目的资源文件下; 15 | - 链接器-输入-附加依赖项要包含SDL2相关的lib文件: 16 | - SDL2.lib 17 | - SDL2main.lib 18 | - SDL2_image.lib 19 | - SDL2_mixer.lib 20 | - SDL2_ttf.lib 21 | - 最后要把SDL2相关的dll文件放在Windows系统目录或exe同一目录下! 22 | 23 | 24 | ### 3、效果演示 25 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_snake/screenshot1.png) 26 | 27 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_snake/screenshot2.png) 28 | 29 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_snake/screenshot3.png) 30 | -------------------------------------------------------------------------------- /SDL_snake/SDL2_ttf.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/SDL2_ttf.dll -------------------------------------------------------------------------------- /SDL_snake/SDLGameObject.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/SDLGameObject.cpp -------------------------------------------------------------------------------- /SDL_snake/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGAMEOBJECT_H__ 2 | #define __SDLGAMEOBJECT_H__ 3 | 4 | #include 5 | #include "gameobject.h" 6 | 7 | using namespace std; 8 | 9 | class SDLGameObject : public GameObject 10 | { 11 | public: 12 | 13 | virtual ~SDLGameObject() {} 14 | 15 | virtual void draw(); 16 | virtual void update(); 17 | virtual void clean(); 18 | virtual void load(std::unique_ptr const &pParams); 19 | 20 | virtual std::string type() { return "SDLGameObject"; } 21 | 22 | protected: 23 | SDLGameObject(); 24 | 25 | 26 | }; 27 | 28 | #endif -------------------------------------------------------------------------------- /SDL_snake/Snake.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/Snake.cpp -------------------------------------------------------------------------------- /SDL_snake/Snake.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/Snake.h -------------------------------------------------------------------------------- /SDL_snake/SoundManager.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // SoundManager.cpp 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 26/03/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #include "SoundManager.h" 10 | 11 | SoundManager* SoundManager::s_pInstance; 12 | 13 | SoundManager::SoundManager() 14 | { 15 | //Mix_OpenAudio(22050, AUDIO_S16, 2, (4096 / 2)); 16 | } 17 | 18 | SoundManager::~SoundManager() 19 | { 20 | Mix_CloseAudio(); 21 | } 22 | 23 | bool SoundManager::AddMusic(std::string fileName, std::string id, sound_type type) 24 | { 25 | if(type == SOUND_MUSIC) 26 | { 27 | Mix_Music* pMusic = Mix_LoadMUS(fileName.c_str()); 28 | if(pMusic == 0) 29 | { 30 | std::cout << "Could not AddMusic music: ERROR - " << Mix_GetError() << std::endl; 31 | return false; 32 | } 33 | 34 | m_music[id] = pMusic; 35 | return true; 36 | } 37 | else if(type == SOUND_SFX) 38 | { 39 | Mix_Chunk* pChunk = Mix_LoadWAV(fileName.c_str()); 40 | if(pChunk == 0) 41 | { 42 | std::cout << "Could not AddMusic SFX: ERROR - " << Mix_GetError() << std::endl; 43 | return false; 44 | } 45 | 46 | m_sfxs[id] = pChunk; 47 | return true; 48 | } 49 | return false; 50 | } 51 | 52 | void SoundManager::playMusic(std::string id, int loop) 53 | { 54 | Mix_PlayMusic(m_music[id], loop); 55 | } 56 | 57 | void SoundManager::playSound(std::string id, int loop) 58 | { 59 | Mix_PlayChannel(-1, m_sfxs[id], loop); 60 | } 61 | -------------------------------------------------------------------------------- /SDL_snake/SoundManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // SoundManager.h 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 26/03/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #ifndef __SoundManager_H__ 10 | #define __SoundManager_H__ 11 | 12 | #include 13 | #include 14 | #include 15 | #include "SDL_mixer.h" 16 | 17 | enum sound_type 18 | { 19 | SOUND_MUSIC = 0, 20 | SOUND_SFX = 1 21 | }; 22 | 23 | class SoundManager 24 | { 25 | public: 26 | 27 | static SoundManager* Instance() 28 | { 29 | if(s_pInstance == 0) 30 | { 31 | s_pInstance = new SoundManager(); 32 | return s_pInstance; 33 | } 34 | return s_pInstance; 35 | } 36 | 37 | bool AddMusic(std::string fileName, std::string id, sound_type type); 38 | 39 | void playSound(std::string id, int loop); 40 | void playMusic(std::string id, int loop); 41 | 42 | private: 43 | 44 | static SoundManager* s_pInstance; 45 | 46 | std::map m_sfxs; 47 | std::map m_music; 48 | 49 | SoundManager(); 50 | ~SoundManager(); 51 | 52 | SoundManager(const SoundManager&); 53 | SoundManager& operator=(const SoundManager&); 54 | }; 55 | 56 | typedef SoundManager TheSoundManager; 57 | 58 | #endif /* defined(__SoundManager_H__) */ 59 | -------------------------------------------------------------------------------- /SDL_snake/TextureManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/TextureManager.cpp -------------------------------------------------------------------------------- /SDL_snake/TextureManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/TextureManager.h -------------------------------------------------------------------------------- /SDL_snake/Vector2D.h: -------------------------------------------------------------------------------- 1 | // 2 | // Vector2D.h 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 27/01/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #ifndef __SDL_Game_Programming_Book__Vector2D__ 10 | #define __SDL_Game_Programming_Book__Vector2D__ 11 | 12 | #include 13 | #include 14 | 15 | class Vector2D 16 | { 17 | public: 18 | Vector2D() 19 | { 20 | m_x = 0; 21 | m_y = 0; 22 | } 23 | 24 | Vector2D(float x, float y): m_x(x), m_y(y) {} 25 | 26 | const float getX() { return m_x; } 27 | const float getY() { return m_y; } 28 | 29 | void setX(float x) { m_x = x; } 30 | void setY(float y) { m_y = y; } 31 | 32 | int length() { return (int)sqrt(m_x * m_x + m_y * m_y); } 33 | 34 | Vector2D operator+(const Vector2D& v2) const { return Vector2D(m_x + v2.m_x, m_y + v2.m_y); } 35 | friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2) 36 | { 37 | v1.m_x += v2.m_x; 38 | v1.m_y += v2.m_y; 39 | 40 | return v1; 41 | } 42 | 43 | Vector2D operator-(const Vector2D& v2) const { return Vector2D(m_x - v2.m_x, m_y - v2.m_y); } 44 | friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2) 45 | { 46 | v1.m_x -= v2.m_x; 47 | v1.m_y -= v2.m_y; 48 | 49 | return v1; 50 | } 51 | 52 | 53 | Vector2D operator*(float scalar) 54 | { 55 | return Vector2D(m_x * scalar, m_y * scalar); 56 | } 57 | 58 | Vector2D& operator*=(float scalar) 59 | { 60 | m_x *= scalar; 61 | m_y *= scalar; 62 | 63 | return *this; 64 | } 65 | 66 | Vector2D operator/(float scalar) 67 | { 68 | return Vector2D(m_x / scalar, m_y / scalar); 69 | } 70 | 71 | Vector2D& operator/=(float scalar) 72 | { 73 | m_x /= scalar; 74 | m_y /= scalar; 75 | 76 | return *this; 77 | } 78 | 79 | 80 | void normalize() 81 | { 82 | int l = length(); 83 | if ( l > 0) 84 | { 85 | (*this) *= 1 / (float)l; 86 | } 87 | } 88 | 89 | float m_x; 90 | float m_y; 91 | }; 92 | 93 | #endif /* defined(__SDL_Game_Programming_Book__Vector2D__) */ 94 | -------------------------------------------------------------------------------- /SDL_snake/gamestate.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/gamestate.h -------------------------------------------------------------------------------- /SDL_snake/gamestatemachine.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/gamestatemachine.cpp -------------------------------------------------------------------------------- /SDL_snake/gamestatemachine.h: -------------------------------------------------------------------------------- 1 | #ifndef __GAMESTATEMACHINE__ 2 | #define __GAMESTATEMACHINE__ 3 | 4 | #include 5 | #include "gamestate.h" 6 | 7 | class GameStateMachine 8 | { 9 | public: 10 | GameStateMachine() {} 11 | ~GameStateMachine() {} 12 | 13 | void update(); 14 | void render(); 15 | 16 | void pushState(GameState* pState); 17 | void changeState(GameState* pState); 18 | void popState(); 19 | 20 | private: 21 | std::vector m_gameStates; 22 | }; 23 | 24 | #endif -------------------------------------------------------------------------------- /SDL_snake/inputhandler.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/inputhandler.cpp -------------------------------------------------------------------------------- /SDL_snake/inputhandler.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/inputhandler.h -------------------------------------------------------------------------------- /SDL_snake/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/main.cpp -------------------------------------------------------------------------------- /SDL_snake/mainmenustate.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/mainmenustate.cpp -------------------------------------------------------------------------------- /SDL_snake/mainmenustate.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAINMENUSTATE__ 2 | #define __MAINMENUSTATE__ 3 | 4 | #include "MenuState.h" 5 | #include "GameObject.h" 6 | 7 | using namespace std; 8 | 9 | class MainMenuState : public MenuState 10 | { 11 | public: 12 | 13 | virtual ~MainMenuState() {} 14 | 15 | virtual void update(); 16 | virtual void render(); 17 | 18 | virtual bool onEnter(); 19 | virtual bool onExit(); 20 | 21 | virtual std::string getStateID() const { return s_menuID; } 22 | 23 | private: 24 | 25 | virtual void setCallbacks(const std::vector& callbacks); 26 | 27 | // call back functions for menu items 28 | static void s_menuToPlay(); 29 | static void s_exitFromMenu(); 30 | 31 | static const std::string s_menuID; 32 | vector m_gameObjects; 33 | }; 34 | 35 | #endif -------------------------------------------------------------------------------- /SDL_snake/menubutton.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/menubutton.cpp -------------------------------------------------------------------------------- /SDL_snake/menubutton.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/menubutton.h -------------------------------------------------------------------------------- /SDL_snake/menustate.h: -------------------------------------------------------------------------------- 1 | // 2 | // MenuState.h 3 | // SDL Game Programming Book 4 | // 5 | // Created by shaun mitchell on 26/02/2013. 6 | // Copyright (c) 2013 shaun mitchell. All rights reserved. 7 | // 8 | 9 | #ifndef __MENUSTATE__ 10 | #define __MENUSTATE__ 11 | 12 | #include 13 | #include "GameState.h" 14 | 15 | class MenuState : public GameState 16 | { 17 | public: 18 | 19 | virtual ~MenuState() {} 20 | 21 | protected: 22 | 23 | typedef void(*Callback)(); 24 | virtual void setCallbacks(const std::vector& callbacks) = 0; 25 | 26 | std::vector m_callbacks; 27 | }; 28 | 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /SDL_snake/pausestate.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/pausestate.cpp -------------------------------------------------------------------------------- /SDL_snake/pausestate.h: -------------------------------------------------------------------------------- 1 | #ifndef __PAUSESTATE__ 2 | #define __PAUSESTATE__ 3 | 4 | #include "menustate.h" 5 | #include 6 | 7 | class GameObject; 8 | 9 | class PauseState : public MenuState 10 | { 11 | public: 12 | 13 | virtual ~PauseState(){} 14 | 15 | virtual void update(); 16 | virtual void render(); 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_pauseID; } 21 | 22 | virtual void setCallbacks(const std::vector& callbacks); 23 | 24 | private: 25 | static void s_pauseToMain(); 26 | static void s_resumePlay(); 27 | 28 | static const std::string s_pauseID; 29 | 30 | std::vector m_gameObjects; 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /SDL_snake/playstate.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/playstate.cpp -------------------------------------------------------------------------------- /SDL_snake/playstate.h: -------------------------------------------------------------------------------- 1 | #ifndef __PLAYSTATE__ 2 | #define __PLAYSTATE__ 3 | 4 | #include "gamestate.h" 5 | #include 6 | //#include "level.h" 7 | 8 | class GameObject; 9 | 10 | class PlayState : public GameState 11 | { 12 | public: 13 | 14 | virtual ~PlayState(){} 15 | 16 | virtual void update(); 17 | virtual void render(); 18 | 19 | virtual bool onEnter(); 20 | virtual bool onExit(); 21 | 22 | virtual std::string getStateID() const { return s_playID; } 23 | 24 | private: 25 | static const std::string s_playID; 26 | 27 | std::vector m_gameObjects; 28 | 29 | //Level* pLevel; 30 | }; 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /SDL_snake/res/backgroundmusic.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/backgroundmusic.wav -------------------------------------------------------------------------------- /SDL_snake/res/button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/button.png -------------------------------------------------------------------------------- /SDL_snake/res/exit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/exit.png -------------------------------------------------------------------------------- /SDL_snake/res/gameover.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/gameover.mp3 -------------------------------------------------------------------------------- /SDL_snake/res/gameover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/gameover.png -------------------------------------------------------------------------------- /SDL_snake/res/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/image.png -------------------------------------------------------------------------------- /SDL_snake/res/lazy.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/lazy.ttf -------------------------------------------------------------------------------- /SDL_snake/res/main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/main.png -------------------------------------------------------------------------------- /SDL_snake/res/mouseclickbutton.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/mouseclickbutton.wav -------------------------------------------------------------------------------- /SDL_snake/res/mousemovetobutton.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/mousemovetobutton.wav -------------------------------------------------------------------------------- /SDL_snake/res/restart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/restart.png -------------------------------------------------------------------------------- /SDL_snake/res/resume.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/res/resume.png -------------------------------------------------------------------------------- /SDL_snake/screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/screenshot1.png -------------------------------------------------------------------------------- /SDL_snake/screenshot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/screenshot2.png -------------------------------------------------------------------------------- /SDL_snake/screenshot3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/screenshot3.png -------------------------------------------------------------------------------- /SDL_snake/stateparser.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/stateparser.cpp -------------------------------------------------------------------------------- /SDL_snake/stateparser.h: -------------------------------------------------------------------------------- 1 | #ifndef __STATEPARSER__ 2 | #define __STATEPARSER__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class GameObject; 9 | 10 | class StateParser 11 | { 12 | public: 13 | 14 | StateParser() {} 15 | ~StateParser() {} 16 | 17 | bool parseState(const char* stateFile, std::string stateID,std::vector *pObjects, std::vector *pTextureIDs); 18 | 19 | void parseObjects(TiXmlElement* pStateRoot, std::vector *pObjects); 20 | 21 | void parseTextures(TiXmlElement* pStateRoot, std::vector *pTextureIDs); 22 | }; 23 | 24 | #endif -------------------------------------------------------------------------------- /SDL_snake/test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /SDL_snake/tinyxml/changes.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/tinyxml/changes.txt -------------------------------------------------------------------------------- /SDL_snake/tinyxml/readme.txt: -------------------------------------------------------------------------------- 1 | /** @mainpage 2 | 3 |

TinyXML

4 | 5 | TinyXML is a simple, small, C++ XML parser that can be easily 6 | integrated into other programs. 7 | 8 |

What it does.

9 | 10 | In brief, TinyXML parses an XML document, and builds from that a 11 | Document Object Model (DOM) that can be read, modified, and saved. 12 | 13 | XML stands for "eXtensible Markup Language." It allows you to create 14 | your own document markups. Where HTML does a very good job of marking 15 | documents for browsers, XML allows you to define any kind of document 16 | markup, for example a document that describes a "to do" list for an 17 | organizer application. XML is a very structured and convenient format. 18 | All those random file formats created to store application data can 19 | all be replaced with XML. One parser for everything. 20 | 21 | The best place for the complete, correct, and quite frankly hard to 22 | read spec is at 23 | http://www.w3.org/TR/2004/REC-xml-20040204/. An intro to XML 24 | (that I really like) can be found at 25 | http://skew.org/xml/tutorial. 26 | 27 | There are different ways to access and interact with XML data. 28 | TinyXML uses a Document Object Model (DOM), meaning the XML data is parsed 29 | into a C++ objects that can be browsed and manipulated, and then 30 | written to disk or another output stream. You can also construct an XML document 31 | from scratch with C++ objects and write this to disk or another output 32 | stream. 33 | 34 | TinyXML is designed to be easy and fast to learn. It is two headers 35 | and four cpp files. Simply add these to your project and off you go. 36 | There is an example file - xmltest.cpp - to get you started. 37 | 38 | TinyXML is released under the ZLib license, 39 | so you can use it in open source or commercial code. The details 40 | of the license are at the top of every source file. 41 | 42 | TinyXML attempts to be a flexible parser, but with truly correct and 43 | compliant XML output. TinyXML should compile on any reasonably C++ 44 | compliant system. It does not rely on exceptions or RTTI. It can be 45 | compiled with or without STL support. TinyXML fully supports 46 | the UTF-8 encoding, and the first 64k character entities. 47 | 48 | 49 |

What it doesn't do.

50 | 51 | TinyXML doesn't parse or use DTDs (Document Type Definitions) or XSLs 52 | (eXtensible Stylesheet Language.) There are other parsers out there 53 | (check out www.sourceforge.org, search for XML) that are much more fully 54 | featured. But they are also much bigger, take longer to set up in 55 | your project, have a higher learning curve, and often have a more 56 | restrictive license. If you are working with browsers or have more 57 | complete XML needs, TinyXML is not the parser for you. 58 | 59 | The following DTD syntax will not parse at this time in TinyXML: 60 | 61 | @verbatim 62 | 64 | ]> 65 | @endverbatim 66 | 67 | because TinyXML sees this as a !DOCTYPE node with an illegally 68 | embedded !ELEMENT node. This may be addressed in the future. 69 | 70 |

Tutorials.

71 | 72 | For the impatient, here is a tutorial to get you going. A great way to get started, 73 | but it is worth your time to read this (very short) manual completely. 74 | 75 | - @subpage tutorial0 76 | 77 |

Code Status.

78 | 79 | TinyXML is mature, tested code. It is very stable. If you find 80 | bugs, please file a bug report on the sourceforge web site 81 | (www.sourceforge.net/projects/tinyxml). We'll get them straightened 82 | out as soon as possible. 83 | 84 | There are some areas of improvement; please check sourceforge if you are 85 | interested in working on TinyXML. 86 | 87 |

Related Projects

88 | 89 | TinyXML projects you may find useful! (Descriptions provided by the projects.) 90 | 91 |
    92 |
  • TinyXPath (http://tinyxpath.sourceforge.net). TinyXPath is a small footprint 93 | XPath syntax decoder, written in C++.
  • 94 |
  • TinyXML++ (http://code.google.com/p/ticpp/). TinyXML++ is a completely new 95 | interface to TinyXML that uses MANY of the C++ strengths. Templates, 96 | exceptions, and much better error handling.
  • 97 |
98 | 99 |

Features

100 | 101 |

Using STL

102 | 103 | TinyXML can be compiled to use or not use STL. When using STL, TinyXML 104 | uses the std::string class, and fully supports std::istream, std::ostream, 105 | operator<<, and operator>>. Many API methods have both 'const char*' and 106 | 'const std::string&' forms. 107 | 108 | When STL support is compiled out, no STL files are included whatsoever. All 109 | the string classes are implemented by TinyXML itself. API methods 110 | all use the 'const char*' form for input. 111 | 112 | Use the compile time #define: 113 | 114 | TIXML_USE_STL 115 | 116 | to compile one version or the other. This can be passed by the compiler, 117 | or set as the first line of "tinyxml.h". 118 | 119 | Note: If compiling the test code in Linux, setting the environment 120 | variable TINYXML_USE_STL=YES/NO will control STL compilation. In the 121 | Windows project file, STL and non STL targets are provided. In your project, 122 | It's probably easiest to add the line "#define TIXML_USE_STL" as the first 123 | line of tinyxml.h. 124 | 125 |

UTF-8

126 | 127 | TinyXML supports UTF-8 allowing to manipulate XML files in any language. TinyXML 128 | also supports "legacy mode" - the encoding used before UTF-8 support and 129 | probably best described as "extended ascii". 130 | 131 | Normally, TinyXML will try to detect the correct encoding and use it. However, 132 | by setting the value of TIXML_DEFAULT_ENCODING in the header file, TinyXML 133 | can be forced to always use one encoding. 134 | 135 | TinyXML will assume Legacy Mode until one of the following occurs: 136 |
    137 |
  1. If the non-standard but common "UTF-8 lead bytes" (0xef 0xbb 0xbf) 138 | begin the file or data stream, TinyXML will read it as UTF-8.
  2. 139 |
  3. If the declaration tag is read, and it has an encoding="UTF-8", then 140 | TinyXML will read it as UTF-8.
  4. 141 |
  5. If the declaration tag is read, and it has no encoding specified, then TinyXML will 142 | read it as UTF-8.
  6. 143 |
  7. If the declaration tag is read, and it has an encoding="something else", then TinyXML 144 | will read it as Legacy Mode. In legacy mode, TinyXML will work as it did before. It's 145 | not clear what that mode does exactly, but old content should keep working.
  8. 146 |
  9. Until one of the above criteria is met, TinyXML runs in Legacy Mode.
  10. 147 |
148 | 149 | What happens if the encoding is incorrectly set or detected? TinyXML will try 150 | to read and pass through text seen as improperly encoded. You may get some strange results or 151 | mangled characters. You may want to force TinyXML to the correct mode. 152 | 153 | You may force TinyXML to Legacy Mode by using LoadFile( TIXML_ENCODING_LEGACY ) or 154 | LoadFile( filename, TIXML_ENCODING_LEGACY ). You may force it to use legacy mode all 155 | the time by setting TIXML_DEFAULT_ENCODING = TIXML_ENCODING_LEGACY. Likewise, you may 156 | force it to TIXML_ENCODING_UTF8 with the same technique. 157 | 158 | For English users, using English XML, UTF-8 is the same as low-ASCII. You 159 | don't need to be aware of UTF-8 or change your code in any way. You can think 160 | of UTF-8 as a "superset" of ASCII. 161 | 162 | UTF-8 is not a double byte format - but it is a standard encoding of Unicode! 163 | TinyXML does not use or directly support wchar, TCHAR, or Microsoft's _UNICODE at this time. 164 | It is common to see the term "Unicode" improperly refer to UTF-16, a wide byte encoding 165 | of unicode. This is a source of confusion. 166 | 167 | For "high-ascii" languages - everything not English, pretty much - TinyXML can 168 | handle all languages, at the same time, as long as the XML is encoded 169 | in UTF-8. That can be a little tricky, older programs and operating systems 170 | tend to use the "default" or "traditional" code page. Many apps (and almost all 171 | modern ones) can output UTF-8, but older or stubborn (or just broken) ones 172 | still output text in the default code page. 173 | 174 | For example, Japanese systems traditionally use SHIFT-JIS encoding. 175 | Text encoded as SHIFT-JIS can not be read by TinyXML. 176 | A good text editor can import SHIFT-JIS and then save as UTF-8. 177 | 178 | The Skew.org link does a great 179 | job covering the encoding issue. 180 | 181 | The test file "utf8test.xml" is an XML containing English, Spanish, Russian, 182 | and Simplified Chinese. (Hopefully they are translated correctly). The file 183 | "utf8test.gif" is a screen capture of the XML file, rendered in IE. Note that 184 | if you don't have the correct fonts (Simplified Chinese or Russian) on your 185 | system, you won't see output that matches the GIF file even if you can parse 186 | it correctly. Also note that (at least on my Windows machine) console output 187 | is in a Western code page, so that Print() or printf() cannot correctly display 188 | the file. This is not a bug in TinyXML - just an OS issue. No data is lost or 189 | destroyed by TinyXML. The console just doesn't render UTF-8. 190 | 191 | 192 |

Entities

193 | TinyXML recognizes the pre-defined "character entities", meaning special 194 | characters. Namely: 195 | 196 | @verbatim 197 | & & 198 | < < 199 | > > 200 | " " 201 | ' ' 202 | @endverbatim 203 | 204 | These are recognized when the XML document is read, and translated to there 205 | UTF-8 equivalents. For instance, text with the XML of: 206 | 207 | @verbatim 208 | Far & Away 209 | @endverbatim 210 | 211 | will have the Value() of "Far & Away" when queried from the TiXmlText object, 212 | and will be written back to the XML stream/file as an ampersand. Older versions 213 | of TinyXML "preserved" character entities, but the newer versions will translate 214 | them into characters. 215 | 216 | Additionally, any character can be specified by its Unicode code point: 217 | The syntax " " or " " are both to the non-breaking space characher. 218 | 219 |

Printing

220 | TinyXML can print output in several different ways that all have strengths and limitations. 221 | 222 | - Print( FILE* ). Output to a std-C stream, which includes all C files as well as stdout. 223 | - "Pretty prints", but you don't have control over printing options. 224 | - The output is streamed directly to the FILE object, so there is no memory overhead 225 | in the TinyXML code. 226 | - used by Print() and SaveFile() 227 | 228 | - operator<<. Output to a c++ stream. 229 | - Integrates with standart C++ iostreams. 230 | - Outputs in "network printing" mode without line breaks. Good for network transmission 231 | and moving XML between C++ objects, but hard for a human to read. 232 | 233 | - TiXmlPrinter. Output to a std::string or memory buffer. 234 | - API is less concise 235 | - Future printing options will be put here. 236 | - Printing may change slightly in future versions as it is refined and expanded. 237 | 238 |

Streams

239 | With TIXML_USE_STL on TinyXML supports C++ streams (operator <<,>>) streams as well 240 | as C (FILE*) streams. There are some differences that you may need to be aware of. 241 | 242 | C style output: 243 | - based on FILE* 244 | - the Print() and SaveFile() methods 245 | 246 | Generates formatted output, with plenty of white space, intended to be as 247 | human-readable as possible. They are very fast, and tolerant of ill formed 248 | XML documents. For example, an XML document that contains 2 root elements 249 | and 2 declarations, will still print. 250 | 251 | C style input: 252 | - based on FILE* 253 | - the Parse() and LoadFile() methods 254 | 255 | A fast, tolerant read. Use whenever you don't need the C++ streams. 256 | 257 | C++ style output: 258 | - based on std::ostream 259 | - operator<< 260 | 261 | Generates condensed output, intended for network transmission rather than 262 | readability. Depending on your system's implementation of the ostream class, 263 | these may be somewhat slower. (Or may not.) Not tolerant of ill formed XML: 264 | a document should contain the correct one root element. Additional root level 265 | elements will not be streamed out. 266 | 267 | C++ style input: 268 | - based on std::istream 269 | - operator>> 270 | 271 | Reads XML from a stream, making it useful for network transmission. The tricky 272 | part is knowing when the XML document is complete, since there will almost 273 | certainly be other data in the stream. TinyXML will assume the XML data is 274 | complete after it reads the root element. Put another way, documents that 275 | are ill-constructed with more than one root element will not read correctly. 276 | Also note that operator>> is somewhat slower than Parse, due to both 277 | implementation of the STL and limitations of TinyXML. 278 | 279 |

White space

280 | The world simply does not agree on whether white space should be kept, or condensed. 281 | For example, pretend the '_' is a space, and look at "Hello____world". HTML, and 282 | at least some XML parsers, will interpret this as "Hello_world". They condense white 283 | space. Some XML parsers do not, and will leave it as "Hello____world". (Remember 284 | to keep pretending the _ is a space.) Others suggest that __Hello___world__ should become 285 | Hello___world. 286 | 287 | It's an issue that hasn't been resolved to my satisfaction. TinyXML supports the 288 | first 2 approaches. Call TiXmlBase::SetCondenseWhiteSpace( bool ) to set the desired behavior. 289 | The default is to condense white space. 290 | 291 | If you change the default, you should call TiXmlBase::SetCondenseWhiteSpace( bool ) 292 | before making any calls to Parse XML data, and I don't recommend changing it after 293 | it has been set. 294 | 295 | 296 |

Handles

297 | 298 | Where browsing an XML document in a robust way, it is important to check 299 | for null returns from method calls. An error safe implementation can 300 | generate a lot of code like: 301 | 302 | @verbatim 303 | TiXmlElement* root = document.FirstChildElement( "Document" ); 304 | if ( root ) 305 | { 306 | TiXmlElement* element = root->FirstChildElement( "Element" ); 307 | if ( element ) 308 | { 309 | TiXmlElement* child = element->FirstChildElement( "Child" ); 310 | if ( child ) 311 | { 312 | TiXmlElement* child2 = child->NextSiblingElement( "Child" ); 313 | if ( child2 ) 314 | { 315 | // Finally do something useful. 316 | @endverbatim 317 | 318 | Handles have been introduced to clean this up. Using the TiXmlHandle class, 319 | the previous code reduces to: 320 | 321 | @verbatim 322 | TiXmlHandle docHandle( &document ); 323 | TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement(); 324 | if ( child2 ) 325 | { 326 | // do something useful 327 | @endverbatim 328 | 329 | Which is much easier to deal with. See TiXmlHandle for more information. 330 | 331 | 332 |

Row and Column tracking

333 | Being able to track nodes and attributes back to their origin location 334 | in source files can be very important for some applications. Additionally, 335 | knowing where parsing errors occured in the original source can be very 336 | time saving. 337 | 338 | TinyXML can tracks the row and column origin of all nodes and attributes 339 | in a text file. The TiXmlBase::Row() and TiXmlBase::Column() methods return 340 | the origin of the node in the source text. The correct tabs can be 341 | configured in TiXmlDocument::SetTabSize(). 342 | 343 | 344 |

Using and Installing

345 | 346 | To Compile and Run xmltest: 347 | 348 | A Linux Makefile and a Windows Visual C++ .dsw file is provided. 349 | Simply compile and run. It will write the file demotest.xml to your 350 | disk and generate output on the screen. It also tests walking the 351 | DOM by printing out the number of nodes found using different 352 | techniques. 353 | 354 | The Linux makefile is very generic and runs on many systems - it 355 | is currently tested on mingw and 356 | MacOSX. You do not need to run 'make depend'. The dependecies have been 357 | hard coded. 358 | 359 |

Windows project file for VC6

360 |
    361 |
  • tinyxml: tinyxml library, non-STL
  • 362 |
  • tinyxmlSTL: tinyxml library, STL
  • 363 |
  • tinyXmlTest: test app, non-STL
  • 364 |
  • tinyXmlTestSTL: test app, STL
  • 365 |
366 | 367 |

Makefile

368 | At the top of the makefile you can set: 369 | 370 | PROFILE, DEBUG, and TINYXML_USE_STL. Details (such that they are) are in 371 | the makefile. 372 | 373 | In the tinyxml directory, type "make clean" then "make". The executable 374 | file 'xmltest' will be created. 375 | 376 | 377 | 378 |

To Use in an Application:

379 | 380 | Add tinyxml.cpp, tinyxml.h, tinyxmlerror.cpp, tinyxmlparser.cpp, tinystr.cpp, and tinystr.h to your 381 | project or make file. That's it! It should compile on any reasonably 382 | compliant C++ system. You do not need to enable exceptions or 383 | RTTI for TinyXML. 384 | 385 | 386 |

How TinyXML works.

387 | 388 | An example is probably the best way to go. Take: 389 | @verbatim 390 | 391 | 392 | 393 | Go to the Toy store! 394 | Do bills 395 | 396 | @endverbatim 397 | 398 | Its not much of a To Do list, but it will do. To read this file 399 | (say "demo.xml") you would create a document, and parse it in: 400 | @verbatim 401 | TiXmlDocument doc( "demo.xml" ); 402 | doc.LoadFile(); 403 | @endverbatim 404 | 405 | And its ready to go. Now lets look at some lines and how they 406 | relate to the DOM. 407 | 408 | @verbatim 409 | 410 | @endverbatim 411 | 412 | The first line is a declaration, and gets turned into the 413 | TiXmlDeclaration class. It will be the first child of the 414 | document node. 415 | 416 | This is the only directive/special tag parsed by TinyXML. 417 | Generally directive tags are stored in TiXmlUnknown so the 418 | commands wont be lost when it is saved back to disk. 419 | 420 | @verbatim 421 | 422 | @endverbatim 423 | 424 | A comment. Will become a TiXmlComment object. 425 | 426 | @verbatim 427 | 428 | @endverbatim 429 | 430 | The "ToDo" tag defines a TiXmlElement object. This one does not have 431 | any attributes, but does contain 2 other elements. 432 | 433 | @verbatim 434 | 435 | @endverbatim 436 | 437 | Creates another TiXmlElement which is a child of the "ToDo" element. 438 | This element has 1 attribute, with the name "priority" and the value 439 | "1". 440 | 441 | @verbatim 442 | Go to the 443 | @endverbatim 444 | 445 | A TiXmlText. This is a leaf node and cannot contain other nodes. 446 | It is a child of the "Item" TiXmlElement. 447 | 448 | @verbatim 449 | 450 | @endverbatim 451 | 452 | 453 | Another TiXmlElement, this one a child of the "Item" element. 454 | 455 | Etc. 456 | 457 | Looking at the entire object tree, you end up with: 458 | @verbatim 459 | TiXmlDocument "demo.xml" 460 | TiXmlDeclaration "version='1.0'" "standalone=no" 461 | TiXmlComment " Our to do list data" 462 | TiXmlElement "ToDo" 463 | TiXmlElement "Item" Attribtutes: priority = 1 464 | TiXmlText "Go to the " 465 | TiXmlElement "bold" 466 | TiXmlText "Toy store!" 467 | TiXmlElement "Item" Attributes: priority=2 468 | TiXmlText "Do bills" 469 | @endverbatim 470 | 471 |

Documentation

472 | 473 | The documentation is build with Doxygen, using the 'dox' 474 | configuration file. 475 | 476 |

License

477 | 478 | TinyXML is released under the zlib license: 479 | 480 | This software is provided 'as-is', without any express or implied 481 | warranty. In no event will the authors be held liable for any 482 | damages arising from the use of this software. 483 | 484 | Permission is granted to anyone to use this software for any 485 | purpose, including commercial applications, and to alter it and 486 | redistribute it freely, subject to the following restrictions: 487 | 488 | 1. The origin of this software must not be misrepresented; you must 489 | not claim that you wrote the original software. If you use this 490 | software in a product, an acknowledgment in the product documentation 491 | would be appreciated but is not required. 492 | 493 | 2. Altered source versions must be plainly marked as such, and 494 | must not be misrepresented as being the original software. 495 | 496 | 3. This notice may not be removed or altered from any source 497 | distribution. 498 | 499 |

References

500 | 501 | The World Wide Web Consortium is the definitive standard body for 502 | XML, and their web pages contain huge amounts of information. 503 | 504 | The definitive spec: 505 | http://www.w3.org/TR/2004/REC-xml-20040204/ 506 | 507 | I also recommend "XML Pocket Reference" by Robert Eckstein and published by 508 | OReilly...the book that got the whole thing started. 509 | 510 |

Contributors, Contacts, and a Brief History

511 | 512 | Thanks very much to everyone who sends suggestions, bugs, ideas, and 513 | encouragement. It all helps, and makes this project fun. A special thanks 514 | to the contributors on the web pages that keep it lively. 515 | 516 | So many people have sent in bugs and ideas, that rather than list here 517 | we try to give credit due in the "changes.txt" file. 518 | 519 | TinyXML was originally written by Lee Thomason. (Often the "I" still 520 | in the documentation.) Lee reviews changes and releases new versions, 521 | with the help of Yves Berquin, Andrew Ellerton, and the tinyXml community. 522 | 523 | We appreciate your suggestions, and would love to know if you 524 | use TinyXML. Hopefully you will enjoy it and find it useful. 525 | Please post questions, comments, file bugs, or contact us at: 526 | 527 | www.sourceforge.net/projects/tinyxml 528 | 529 | Lee Thomason, Yves Berquin, Andrew Ellerton 530 | */ 531 | -------------------------------------------------------------------------------- /SDL_snake/tinyxml/tinystr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | www.sourceforge.net/projects/tinyxml 3 | 4 | This software is provided 'as-is', without any express or implied 5 | warranty. In no event will the authors be held liable for any 6 | damages arising from the use of this software. 7 | 8 | Permission is granted to anyone to use this software for any 9 | purpose, including commercial applications, and to alter it and 10 | redistribute it freely, subject to the following restrictions: 11 | 12 | 1. The origin of this software must not be misrepresented; you must 13 | not claim that you wrote the original software. If you use this 14 | software in a product, an acknowledgment in the product documentation 15 | would be appreciated but is not required. 16 | 17 | 2. Altered source versions must be plainly marked as such, and 18 | must not be misrepresented as being the original software. 19 | 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | */ 23 | 24 | 25 | #ifndef TIXML_USE_STL 26 | 27 | #include "tinystr.h" 28 | 29 | // Error value for find primitive 30 | const TiXmlString::size_type TiXmlString::npos = static_cast< TiXmlString::size_type >(-1); 31 | 32 | 33 | // Null rep. 34 | TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, { '\0' } }; 35 | 36 | 37 | void TiXmlString::reserve (size_type cap) 38 | { 39 | if (cap > capacity()) 40 | { 41 | TiXmlString tmp; 42 | tmp.init(length(), cap); 43 | memcpy(tmp.start(), data(), length()); 44 | swap(tmp); 45 | } 46 | } 47 | 48 | 49 | TiXmlString& TiXmlString::assign(const char* str, size_type len) 50 | { 51 | size_type cap = capacity(); 52 | if (len > cap || cap > 3*(len + 8)) 53 | { 54 | TiXmlString tmp; 55 | tmp.init(len); 56 | memcpy(tmp.start(), str, len); 57 | swap(tmp); 58 | } 59 | else 60 | { 61 | memmove(start(), str, len); 62 | set_size(len); 63 | } 64 | return *this; 65 | } 66 | 67 | 68 | TiXmlString& TiXmlString::append(const char* str, size_type len) 69 | { 70 | size_type newsize = length() + len; 71 | if (newsize > capacity()) 72 | { 73 | reserve (newsize + capacity()); 74 | } 75 | memmove(finish(), str, len); 76 | set_size(newsize); 77 | return *this; 78 | } 79 | 80 | 81 | TiXmlString operator + (const TiXmlString & a, const TiXmlString & b) 82 | { 83 | TiXmlString tmp; 84 | tmp.reserve(a.length() + b.length()); 85 | tmp += a; 86 | tmp += b; 87 | return tmp; 88 | } 89 | 90 | TiXmlString operator + (const TiXmlString & a, const char* b) 91 | { 92 | TiXmlString tmp; 93 | TiXmlString::size_type b_len = static_cast( strlen(b) ); 94 | tmp.reserve(a.length() + b_len); 95 | tmp += a; 96 | tmp.append(b, b_len); 97 | return tmp; 98 | } 99 | 100 | TiXmlString operator + (const char* a, const TiXmlString & b) 101 | { 102 | TiXmlString tmp; 103 | TiXmlString::size_type a_len = static_cast( strlen(a) ); 104 | tmp.reserve(a_len + b.length()); 105 | tmp.append(a, a_len); 106 | tmp += b; 107 | return tmp; 108 | } 109 | 110 | 111 | #endif // TIXML_USE_STL 112 | -------------------------------------------------------------------------------- /SDL_snake/tinyxml/tinystr.h: -------------------------------------------------------------------------------- 1 | /* 2 | www.sourceforge.net/projects/tinyxml 3 | 4 | This software is provided 'as-is', without any express or implied 5 | warranty. In no event will the authors be held liable for any 6 | damages arising from the use of this software. 7 | 8 | Permission is granted to anyone to use this software for any 9 | purpose, including commercial applications, and to alter it and 10 | redistribute it freely, subject to the following restrictions: 11 | 12 | 1. The origin of this software must not be misrepresented; you must 13 | not claim that you wrote the original software. If you use this 14 | software in a product, an acknowledgment in the product documentation 15 | would be appreciated but is not required. 16 | 17 | 2. Altered source versions must be plainly marked as such, and 18 | must not be misrepresented as being the original software. 19 | 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | */ 23 | 24 | 25 | #ifndef TIXML_USE_STL 26 | 27 | #ifndef TIXML_STRING_INCLUDED 28 | #define TIXML_STRING_INCLUDED 29 | 30 | #include 31 | #include 32 | 33 | /* The support for explicit isn't that universal, and it isn't really 34 | required - it is used to check that the TiXmlString class isn't incorrectly 35 | used. Be nice to old compilers and macro it here: 36 | */ 37 | #if defined(_MSC_VER) && (_MSC_VER >= 1200 ) 38 | // Microsoft visual studio, version 6 and higher. 39 | #define TIXML_EXPLICIT explicit 40 | #elif defined(__GNUC__) && (__GNUC__ >= 3 ) 41 | // GCC version 3 and higher.s 42 | #define TIXML_EXPLICIT explicit 43 | #else 44 | #define TIXML_EXPLICIT 45 | #endif 46 | 47 | 48 | /* 49 | TiXmlString is an emulation of a subset of the std::string template. 50 | Its purpose is to allow compiling TinyXML on compilers with no or poor STL support. 51 | Only the member functions relevant to the TinyXML project have been implemented. 52 | The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase 53 | a string and there's no more room, we allocate a buffer twice as big as we need. 54 | */ 55 | class TiXmlString 56 | { 57 | public : 58 | // The size type used 59 | typedef size_t size_type; 60 | 61 | // Error value for find primitive 62 | static const size_type npos; // = -1; 63 | 64 | 65 | // TiXmlString empty constructor 66 | TiXmlString () : rep_(&nullrep_) 67 | { 68 | } 69 | 70 | // TiXmlString copy constructor 71 | TiXmlString ( const TiXmlString & copy) : rep_(0) 72 | { 73 | init(copy.length()); 74 | memcpy(start(), copy.data(), length()); 75 | } 76 | 77 | // TiXmlString constructor, based on a string 78 | TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0) 79 | { 80 | init( static_cast( strlen(copy) )); 81 | memcpy(start(), copy, length()); 82 | } 83 | 84 | // TiXmlString constructor, based on a string 85 | TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0) 86 | { 87 | init(len); 88 | memcpy(start(), str, len); 89 | } 90 | 91 | // TiXmlString destructor 92 | ~TiXmlString () 93 | { 94 | quit(); 95 | } 96 | 97 | TiXmlString& operator = (const char * copy) 98 | { 99 | return assign( copy, (size_type)strlen(copy)); 100 | } 101 | 102 | TiXmlString& operator = (const TiXmlString & copy) 103 | { 104 | return assign(copy.start(), copy.length()); 105 | } 106 | 107 | 108 | // += operator. Maps to append 109 | TiXmlString& operator += (const char * suffix) 110 | { 111 | return append(suffix, static_cast( strlen(suffix) )); 112 | } 113 | 114 | // += operator. Maps to append 115 | TiXmlString& operator += (char single) 116 | { 117 | return append(&single, 1); 118 | } 119 | 120 | // += operator. Maps to append 121 | TiXmlString& operator += (const TiXmlString & suffix) 122 | { 123 | return append(suffix.data(), suffix.length()); 124 | } 125 | 126 | 127 | // Convert a TiXmlString into a null-terminated char * 128 | const char * c_str () const { return rep_->str; } 129 | 130 | // Convert a TiXmlString into a char * (need not be null terminated). 131 | const char * data () const { return rep_->str; } 132 | 133 | // Return the length of a TiXmlString 134 | size_type length () const { return rep_->size; } 135 | 136 | // Alias for length() 137 | size_type size () const { return rep_->size; } 138 | 139 | // Checks if a TiXmlString is empty 140 | bool empty () const { return rep_->size == 0; } 141 | 142 | // Return capacity of string 143 | size_type capacity () const { return rep_->capacity; } 144 | 145 | 146 | // single char extraction 147 | const char& at (size_type index) const 148 | { 149 | assert( index < length() ); 150 | return rep_->str[ index ]; 151 | } 152 | 153 | // [] operator 154 | char& operator [] (size_type index) const 155 | { 156 | assert( index < length() ); 157 | return rep_->str[ index ]; 158 | } 159 | 160 | // find a char in a string. Return TiXmlString::npos if not found 161 | size_type find (char lookup) const 162 | { 163 | return find(lookup, 0); 164 | } 165 | 166 | // find a char in a string from an offset. Return TiXmlString::npos if not found 167 | size_type find (char tofind, size_type offset) const 168 | { 169 | if (offset >= length()) return npos; 170 | 171 | for (const char* p = c_str() + offset; *p != '\0'; ++p) 172 | { 173 | if (*p == tofind) return static_cast< size_type >( p - c_str() ); 174 | } 175 | return npos; 176 | } 177 | 178 | void clear () 179 | { 180 | //Lee: 181 | //The original was just too strange, though correct: 182 | // TiXmlString().swap(*this); 183 | //Instead use the quit & re-init: 184 | quit(); 185 | init(0,0); 186 | } 187 | 188 | /* Function to reserve a big amount of data when we know we'll need it. Be aware that this 189 | function DOES NOT clear the content of the TiXmlString if any exists. 190 | */ 191 | void reserve (size_type cap); 192 | 193 | TiXmlString& assign (const char* str, size_type len); 194 | 195 | TiXmlString& append (const char* str, size_type len); 196 | 197 | void swap (TiXmlString& other) 198 | { 199 | Rep* r = rep_; 200 | rep_ = other.rep_; 201 | other.rep_ = r; 202 | } 203 | 204 | private: 205 | 206 | void init(size_type sz) { init(sz, sz); } 207 | void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; } 208 | char* start() const { return rep_->str; } 209 | char* finish() const { return rep_->str + rep_->size; } 210 | 211 | struct Rep 212 | { 213 | size_type size, capacity; 214 | char str[1]; 215 | }; 216 | 217 | void init(size_type sz, size_type cap) 218 | { 219 | if (cap) 220 | { 221 | // Lee: the original form: 222 | // rep_ = static_cast(operator new(sizeof(Rep) + cap)); 223 | // doesn't work in some cases of new being overloaded. Switching 224 | // to the normal allocation, although use an 'int' for systems 225 | // that are overly picky about structure alignment. 226 | const size_type bytesNeeded = sizeof(Rep) + cap; 227 | const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int ); 228 | rep_ = reinterpret_cast( new int[ intsNeeded ] ); 229 | 230 | rep_->str[ rep_->size = sz ] = '\0'; 231 | rep_->capacity = cap; 232 | } 233 | else 234 | { 235 | rep_ = &nullrep_; 236 | } 237 | } 238 | 239 | void quit() 240 | { 241 | if (rep_ != &nullrep_) 242 | { 243 | // The rep_ is really an array of ints. (see the allocator, above). 244 | // Cast it back before delete, so the compiler won't incorrectly call destructors. 245 | delete [] ( reinterpret_cast( rep_ ) ); 246 | } 247 | } 248 | 249 | Rep * rep_; 250 | static Rep nullrep_; 251 | 252 | } ; 253 | 254 | 255 | inline bool operator == (const TiXmlString & a, const TiXmlString & b) 256 | { 257 | return ( a.length() == b.length() ) // optimization on some platforms 258 | && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare 259 | } 260 | inline bool operator < (const TiXmlString & a, const TiXmlString & b) 261 | { 262 | return strcmp(a.c_str(), b.c_str()) < 0; 263 | } 264 | 265 | inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); } 266 | inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; } 267 | inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); } 268 | inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); } 269 | 270 | inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; } 271 | inline bool operator == (const char* a, const TiXmlString & b) { return b == a; } 272 | inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); } 273 | inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); } 274 | 275 | TiXmlString operator + (const TiXmlString & a, const TiXmlString & b); 276 | TiXmlString operator + (const TiXmlString & a, const char* b); 277 | TiXmlString operator + (const char* a, const TiXmlString & b); 278 | 279 | 280 | /* 281 | TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString. 282 | Only the operators that we need for TinyXML have been developped. 283 | */ 284 | class TiXmlOutStream : public TiXmlString 285 | { 286 | public : 287 | 288 | // TiXmlOutStream << operator. 289 | TiXmlOutStream & operator << (const TiXmlString & in) 290 | { 291 | *this += in; 292 | return *this; 293 | } 294 | 295 | // TiXmlOutStream << operator. 296 | TiXmlOutStream & operator << (const char * in) 297 | { 298 | *this += in; 299 | return *this; 300 | } 301 | 302 | } ; 303 | 304 | #endif // TIXML_STRING_INCLUDED 305 | #endif // TIXML_USE_STL 306 | -------------------------------------------------------------------------------- /SDL_snake/tinyxml/tinyxmlerror.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | www.sourceforge.net/projects/tinyxml 3 | Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com) 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any 7 | damages arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any 10 | purpose, including commercial applications, and to alter it and 11 | redistribute it freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must 14 | not claim that you wrote the original software. If you use this 15 | software in a product, an acknowledgment in the product documentation 16 | would be appreciated but is not required. 17 | 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | */ 24 | 25 | #include "tinyxml.h" 26 | 27 | // The goal of the seperate error file is to make the first 28 | // step towards localization. tinyxml (currently) only supports 29 | // english error messages, but the could now be translated. 30 | // 31 | // It also cleans up the code a bit. 32 | // 33 | 34 | const char* TiXmlBase::errorString[ TiXmlBase::TIXML_ERROR_STRING_COUNT ] = 35 | { 36 | "No error", 37 | "Error", 38 | "Failed to open file", 39 | "Error parsing Element.", 40 | "Failed to read Element name", 41 | "Error reading Element value.", 42 | "Error reading Attributes.", 43 | "Error: empty tag.", 44 | "Error reading end tag.", 45 | "Error parsing Unknown.", 46 | "Error parsing Comment.", 47 | "Error parsing Declaration.", 48 | "Error document empty.", 49 | "Error null (0) or unexpected EOF found in input stream.", 50 | "Error parsing CDATA.", 51 | "Error when TiXmlDocument added to document, because TiXmlDocument can only be at the root.", 52 | }; 53 | -------------------------------------------------------------------------------- /SDL_snake/tinyxml/tinyxmlparser.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | www.sourceforge.net/projects/tinyxml 3 | Original code by Lee Thomason (www.grinninglizard.com) 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any 7 | damages arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any 10 | purpose, including commercial applications, and to alter it and 11 | redistribute it freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must 14 | not claim that you wrote the original software. If you use this 15 | software in a product, an acknowledgment in the product documentation 16 | would be appreciated but is not required. 17 | 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | #include "tinyxml.h" 29 | 30 | //#define DEBUG_PARSER 31 | #if defined( DEBUG_PARSER ) 32 | # if defined( DEBUG ) && defined( _MSC_VER ) 33 | # include 34 | # define TIXML_LOG OutputDebugString 35 | # else 36 | # define TIXML_LOG printf 37 | # endif 38 | #endif 39 | 40 | // Note tha "PutString" hardcodes the same list. This 41 | // is less flexible than it appears. Changing the entries 42 | // or order will break putstring. 43 | TiXmlBase::Entity TiXmlBase::entity[ TiXmlBase::NUM_ENTITY ] = 44 | { 45 | { "&", 5, '&' }, 46 | { "<", 4, '<' }, 47 | { ">", 4, '>' }, 48 | { """, 6, '\"' }, 49 | { "'", 6, '\'' } 50 | }; 51 | 52 | // Bunch of unicode info at: 53 | // http://www.unicode.org/faq/utf_bom.html 54 | // Including the basic of this table, which determines the #bytes in the 55 | // sequence from the lead byte. 1 placed for invalid sequences -- 56 | // although the result will be junk, pass it through as much as possible. 57 | // Beware of the non-characters in UTF-8: 58 | // ef bb bf (Microsoft "lead bytes") 59 | // ef bf be 60 | // ef bf bf 61 | 62 | const unsigned char TIXML_UTF_LEAD_0 = 0xefU; 63 | const unsigned char TIXML_UTF_LEAD_1 = 0xbbU; 64 | const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; 65 | 66 | const int TiXmlBase::utf8ByteTable[256] = 67 | { 68 | // 0 1 2 3 4 5 6 7 8 9 a b c d e f 69 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00 70 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10 71 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20 72 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30 73 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 74 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x50 75 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 76 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x70 End of ASCII range 77 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x80 0x80 to 0xc1 invalid 78 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x90 79 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xa0 80 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xb0 81 | 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xc0 0xc2 to 0xdf 2 byte 82 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xd0 83 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xe0 0xe0 to 0xef 3 byte 84 | 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 0xf0 0xf0 to 0xf4 4 byte, 0xf5 and higher invalid 85 | }; 86 | 87 | 88 | void TiXmlBase::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ) 89 | { 90 | const unsigned long BYTE_MASK = 0xBF; 91 | const unsigned long BYTE_MARK = 0x80; 92 | const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; 93 | 94 | if (input < 0x80) 95 | *length = 1; 96 | else if ( input < 0x800 ) 97 | *length = 2; 98 | else if ( input < 0x10000 ) 99 | *length = 3; 100 | else if ( input < 0x200000 ) 101 | *length = 4; 102 | else 103 | { *length = 0; return; } // This code won't covert this correctly anyway. 104 | 105 | output += *length; 106 | 107 | // Scary scary fall throughs. 108 | switch (*length) 109 | { 110 | case 4: 111 | --output; 112 | *output = (char)((input | BYTE_MARK) & BYTE_MASK); 113 | input >>= 6; 114 | case 3: 115 | --output; 116 | *output = (char)((input | BYTE_MARK) & BYTE_MASK); 117 | input >>= 6; 118 | case 2: 119 | --output; 120 | *output = (char)((input | BYTE_MARK) & BYTE_MASK); 121 | input >>= 6; 122 | case 1: 123 | --output; 124 | *output = (char)(input | FIRST_BYTE_MARK[*length]); 125 | } 126 | } 127 | 128 | 129 | /*static*/ int TiXmlBase::IsAlpha( unsigned char anyByte, TiXmlEncoding /*encoding*/ ) 130 | { 131 | // This will only work for low-ascii, everything else is assumed to be a valid 132 | // letter. I'm not sure this is the best approach, but it is quite tricky trying 133 | // to figure out alhabetical vs. not across encoding. So take a very 134 | // conservative approach. 135 | 136 | // if ( encoding == TIXML_ENCODING_UTF8 ) 137 | // { 138 | if ( anyByte < 127 ) 139 | return isalpha( anyByte ); 140 | else 141 | return 1; // What else to do? The unicode set is huge...get the english ones right. 142 | // } 143 | // else 144 | // { 145 | // return isalpha( anyByte ); 146 | // } 147 | } 148 | 149 | 150 | /*static*/ int TiXmlBase::IsAlphaNum( unsigned char anyByte, TiXmlEncoding /*encoding*/ ) 151 | { 152 | // This will only work for low-ascii, everything else is assumed to be a valid 153 | // letter. I'm not sure this is the best approach, but it is quite tricky trying 154 | // to figure out alhabetical vs. not across encoding. So take a very 155 | // conservative approach. 156 | 157 | // if ( encoding == TIXML_ENCODING_UTF8 ) 158 | // { 159 | if ( anyByte < 127 ) 160 | return isalnum( anyByte ); 161 | else 162 | return 1; // What else to do? The unicode set is huge...get the english ones right. 163 | // } 164 | // else 165 | // { 166 | // return isalnum( anyByte ); 167 | // } 168 | } 169 | 170 | 171 | class TiXmlParsingData 172 | { 173 | friend class TiXmlDocument; 174 | public: 175 | void Stamp( const char* now, TiXmlEncoding encoding ); 176 | 177 | const TiXmlCursor& Cursor() const { return cursor; } 178 | 179 | private: 180 | // Only used by the document! 181 | TiXmlParsingData( const char* start, int _tabsize, int row, int col ) 182 | { 183 | assert( start ); 184 | stamp = start; 185 | tabsize = _tabsize; 186 | cursor.row = row; 187 | cursor.col = col; 188 | } 189 | 190 | TiXmlCursor cursor; 191 | const char* stamp; 192 | int tabsize; 193 | }; 194 | 195 | 196 | void TiXmlParsingData::Stamp( const char* now, TiXmlEncoding encoding ) 197 | { 198 | assert( now ); 199 | 200 | // Do nothing if the tabsize is 0. 201 | if ( tabsize < 1 ) 202 | { 203 | return; 204 | } 205 | 206 | // Get the current row, column. 207 | int row = cursor.row; 208 | int col = cursor.col; 209 | const char* p = stamp; 210 | assert( p ); 211 | 212 | while ( p < now ) 213 | { 214 | // Treat p as unsigned, so we have a happy compiler. 215 | const unsigned char* pU = (const unsigned char*)p; 216 | 217 | // Code contributed by Fletcher Dunn: (modified by lee) 218 | switch (*pU) { 219 | case 0: 220 | // We *should* never get here, but in case we do, don't 221 | // advance past the terminating null character, ever 222 | return; 223 | 224 | case '\r': 225 | // bump down to the next line 226 | ++row; 227 | col = 0; 228 | // Eat the character 229 | ++p; 230 | 231 | // Check for \r\n sequence, and treat this as a single character 232 | if (*p == '\n') { 233 | ++p; 234 | } 235 | break; 236 | 237 | case '\n': 238 | // bump down to the next line 239 | ++row; 240 | col = 0; 241 | 242 | // Eat the character 243 | ++p; 244 | 245 | // Check for \n\r sequence, and treat this as a single 246 | // character. (Yes, this bizarre thing does occur still 247 | // on some arcane platforms...) 248 | if (*p == '\r') { 249 | ++p; 250 | } 251 | break; 252 | 253 | case '\t': 254 | // Eat the character 255 | ++p; 256 | 257 | // Skip to next tab stop 258 | col = (col / tabsize + 1) * tabsize; 259 | break; 260 | 261 | case TIXML_UTF_LEAD_0: 262 | if ( encoding == TIXML_ENCODING_UTF8 ) 263 | { 264 | if ( *(p+1) && *(p+2) ) 265 | { 266 | // In these cases, don't advance the column. These are 267 | // 0-width spaces. 268 | if ( *(pU+1)==TIXML_UTF_LEAD_1 && *(pU+2)==TIXML_UTF_LEAD_2 ) 269 | p += 3; 270 | else if ( *(pU+1)==0xbfU && *(pU+2)==0xbeU ) 271 | p += 3; 272 | else if ( *(pU+1)==0xbfU && *(pU+2)==0xbfU ) 273 | p += 3; 274 | else 275 | { p +=3; ++col; } // A normal character. 276 | } 277 | } 278 | else 279 | { 280 | ++p; 281 | ++col; 282 | } 283 | break; 284 | 285 | default: 286 | if ( encoding == TIXML_ENCODING_UTF8 ) 287 | { 288 | // Eat the 1 to 4 byte utf8 character. 289 | int step = TiXmlBase::utf8ByteTable[*((const unsigned char*)p)]; 290 | if ( step == 0 ) 291 | step = 1; // Error case from bad encoding, but handle gracefully. 292 | p += step; 293 | 294 | // Just advance one column, of course. 295 | ++col; 296 | } 297 | else 298 | { 299 | ++p; 300 | ++col; 301 | } 302 | break; 303 | } 304 | } 305 | cursor.row = row; 306 | cursor.col = col; 307 | assert( cursor.row >= -1 ); 308 | assert( cursor.col >= -1 ); 309 | stamp = p; 310 | assert( stamp ); 311 | } 312 | 313 | 314 | const char* TiXmlBase::SkipWhiteSpace( const char* p, TiXmlEncoding encoding ) 315 | { 316 | if ( !p || !*p ) 317 | { 318 | return 0; 319 | } 320 | if ( encoding == TIXML_ENCODING_UTF8 ) 321 | { 322 | while ( *p ) 323 | { 324 | const unsigned char* pU = (const unsigned char*)p; 325 | 326 | // Skip the stupid Microsoft UTF-8 Byte order marks 327 | if ( *(pU+0)==TIXML_UTF_LEAD_0 328 | && *(pU+1)==TIXML_UTF_LEAD_1 329 | && *(pU+2)==TIXML_UTF_LEAD_2 ) 330 | { 331 | p += 3; 332 | continue; 333 | } 334 | else if(*(pU+0)==TIXML_UTF_LEAD_0 335 | && *(pU+1)==0xbfU 336 | && *(pU+2)==0xbeU ) 337 | { 338 | p += 3; 339 | continue; 340 | } 341 | else if(*(pU+0)==TIXML_UTF_LEAD_0 342 | && *(pU+1)==0xbfU 343 | && *(pU+2)==0xbfU ) 344 | { 345 | p += 3; 346 | continue; 347 | } 348 | 349 | if ( IsWhiteSpace( *p ) ) // Still using old rules for white space. 350 | ++p; 351 | else 352 | break; 353 | } 354 | } 355 | else 356 | { 357 | while ( *p && IsWhiteSpace( *p ) ) 358 | ++p; 359 | } 360 | 361 | return p; 362 | } 363 | 364 | #ifdef TIXML_USE_STL 365 | /*static*/ bool TiXmlBase::StreamWhiteSpace( std::istream * in, TIXML_STRING * tag ) 366 | { 367 | for( ;; ) 368 | { 369 | if ( !in->good() ) return false; 370 | 371 | int c = in->peek(); 372 | // At this scope, we can't get to a document. So fail silently. 373 | if ( !IsWhiteSpace( c ) || c <= 0 ) 374 | return true; 375 | 376 | *tag += (char) in->get(); 377 | } 378 | } 379 | 380 | /*static*/ bool TiXmlBase::StreamTo( std::istream * in, int character, TIXML_STRING * tag ) 381 | { 382 | //assert( character > 0 && character < 128 ); // else it won't work in utf-8 383 | while ( in->good() ) 384 | { 385 | int c = in->peek(); 386 | if ( c == character ) 387 | return true; 388 | if ( c <= 0 ) // Silent failure: can't get document at this scope 389 | return false; 390 | 391 | in->get(); 392 | *tag += (char) c; 393 | } 394 | return false; 395 | } 396 | #endif 397 | 398 | // One of TinyXML's more performance demanding functions. Try to keep the memory overhead down. The 399 | // "assign" optimization removes over 10% of the execution time. 400 | // 401 | const char* TiXmlBase::ReadName( const char* p, TIXML_STRING * name, TiXmlEncoding encoding ) 402 | { 403 | // Oddly, not supported on some comilers, 404 | //name->clear(); 405 | // So use this: 406 | *name = ""; 407 | assert( p ); 408 | 409 | // Names start with letters or underscores. 410 | // Of course, in unicode, tinyxml has no idea what a letter *is*. The 411 | // algorithm is generous. 412 | // 413 | // After that, they can be letters, underscores, numbers, 414 | // hyphens, or colons. (Colons are valid ony for namespaces, 415 | // but tinyxml can't tell namespaces from names.) 416 | if ( p && *p 417 | && ( IsAlpha( (unsigned char) *p, encoding ) || *p == '_' ) ) 418 | { 419 | const char* start = p; 420 | while( p && *p 421 | && ( IsAlphaNum( (unsigned char ) *p, encoding ) 422 | || *p == '_' 423 | || *p == '-' 424 | || *p == '.' 425 | || *p == ':' ) ) 426 | { 427 | //(*name) += *p; // expensive 428 | ++p; 429 | } 430 | if ( p-start > 0 ) { 431 | name->assign( start, p-start ); 432 | } 433 | return p; 434 | } 435 | return 0; 436 | } 437 | 438 | const char* TiXmlBase::GetEntity( const char* p, char* value, int* length, TiXmlEncoding encoding ) 439 | { 440 | // Presume an entity, and pull it out. 441 | TIXML_STRING ent; 442 | int i; 443 | *length = 0; 444 | 445 | if ( *(p+1) && *(p+1) == '#' && *(p+2) ) 446 | { 447 | unsigned long ucs = 0; 448 | ptrdiff_t delta = 0; 449 | unsigned mult = 1; 450 | 451 | if ( *(p+2) == 'x' ) 452 | { 453 | // Hexadecimal. 454 | if ( !*(p+3) ) return 0; 455 | 456 | const char* q = p+3; 457 | q = strchr( q, ';' ); 458 | 459 | if ( !q || !*q ) return 0; 460 | 461 | delta = q-p; 462 | --q; 463 | 464 | while ( *q != 'x' ) 465 | { 466 | if ( *q >= '0' && *q <= '9' ) 467 | ucs += mult * (*q - '0'); 468 | else if ( *q >= 'a' && *q <= 'f' ) 469 | ucs += mult * (*q - 'a' + 10); 470 | else if ( *q >= 'A' && *q <= 'F' ) 471 | ucs += mult * (*q - 'A' + 10 ); 472 | else 473 | return 0; 474 | mult *= 16; 475 | --q; 476 | } 477 | } 478 | else 479 | { 480 | // Decimal. 481 | if ( !*(p+2) ) return 0; 482 | 483 | const char* q = p+2; 484 | q = strchr( q, ';' ); 485 | 486 | if ( !q || !*q ) return 0; 487 | 488 | delta = q-p; 489 | --q; 490 | 491 | while ( *q != '#' ) 492 | { 493 | if ( *q >= '0' && *q <= '9' ) 494 | ucs += mult * (*q - '0'); 495 | else 496 | return 0; 497 | mult *= 10; 498 | --q; 499 | } 500 | } 501 | if ( encoding == TIXML_ENCODING_UTF8 ) 502 | { 503 | // convert the UCS to UTF-8 504 | ConvertUTF32ToUTF8( ucs, value, length ); 505 | } 506 | else 507 | { 508 | *value = (char)ucs; 509 | *length = 1; 510 | } 511 | return p + delta + 1; 512 | } 513 | 514 | // Now try to match it. 515 | for( i=0; iappend( cArr, len ); 594 | } 595 | } 596 | else 597 | { 598 | bool whitespace = false; 599 | 600 | // Remove leading white space: 601 | p = SkipWhiteSpace( p, encoding ); 602 | while ( p && *p 603 | && !StringEqual( p, endTag, caseInsensitive, encoding ) ) 604 | { 605 | if ( *p == '\r' || *p == '\n' ) 606 | { 607 | whitespace = true; 608 | ++p; 609 | } 610 | else if ( IsWhiteSpace( *p ) ) 611 | { 612 | whitespace = true; 613 | ++p; 614 | } 615 | else 616 | { 617 | // If we've found whitespace, add it before the 618 | // new character. Any whitespace just becomes a space. 619 | if ( whitespace ) 620 | { 621 | (*text) += ' '; 622 | whitespace = false; 623 | } 624 | int len; 625 | char cArr[4] = { 0, 0, 0, 0 }; 626 | p = GetChar( p, cArr, &len, encoding ); 627 | if ( len == 1 ) 628 | (*text) += cArr[0]; // more efficient 629 | else 630 | text->append( cArr, len ); 631 | } 632 | } 633 | } 634 | if ( p && *p ) 635 | p += strlen( endTag ); 636 | return ( p && *p ) ? p : 0; 637 | } 638 | 639 | #ifdef TIXML_USE_STL 640 | 641 | void TiXmlDocument::StreamIn( std::istream * in, TIXML_STRING * tag ) 642 | { 643 | // The basic issue with a document is that we don't know what we're 644 | // streaming. Read something presumed to be a tag (and hope), then 645 | // identify it, and call the appropriate stream method on the tag. 646 | // 647 | // This "pre-streaming" will never read the closing ">" so the 648 | // sub-tag can orient itself. 649 | 650 | if ( !StreamTo( in, '<', tag ) ) 651 | { 652 | SetError( TIXML_ERROR_PARSING_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN ); 653 | return; 654 | } 655 | 656 | while ( in->good() ) 657 | { 658 | int tagIndex = (int) tag->length(); 659 | while ( in->good() && in->peek() != '>' ) 660 | { 661 | int c = in->get(); 662 | if ( c <= 0 ) 663 | { 664 | SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN ); 665 | break; 666 | } 667 | (*tag) += (char) c; 668 | } 669 | 670 | if ( in->good() ) 671 | { 672 | // We now have something we presume to be a node of 673 | // some sort. Identify it, and call the node to 674 | // continue streaming. 675 | TiXmlNode* node = Identify( tag->c_str() + tagIndex, TIXML_DEFAULT_ENCODING ); 676 | 677 | if ( node ) 678 | { 679 | node->StreamIn( in, tag ); 680 | bool isElement = node->ToElement() != 0; 681 | delete node; 682 | node = 0; 683 | 684 | // If this is the root element, we're done. Parsing will be 685 | // done by the >> operator. 686 | if ( isElement ) 687 | { 688 | return; 689 | } 690 | } 691 | else 692 | { 693 | SetError( TIXML_ERROR, 0, 0, TIXML_ENCODING_UNKNOWN ); 694 | return; 695 | } 696 | } 697 | } 698 | // We should have returned sooner. 699 | SetError( TIXML_ERROR, 0, 0, TIXML_ENCODING_UNKNOWN ); 700 | } 701 | 702 | #endif 703 | 704 | const char* TiXmlDocument::Parse( const char* p, TiXmlParsingData* prevData, TiXmlEncoding encoding ) 705 | { 706 | ClearError(); 707 | 708 | // Parse away, at the document level. Since a document 709 | // contains nothing but other tags, most of what happens 710 | // here is skipping white space. 711 | if ( !p || !*p ) 712 | { 713 | SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN ); 714 | return 0; 715 | } 716 | 717 | // Note that, for a document, this needs to come 718 | // before the while space skip, so that parsing 719 | // starts from the pointer we are given. 720 | location.Clear(); 721 | if ( prevData ) 722 | { 723 | location.row = prevData->cursor.row; 724 | location.col = prevData->cursor.col; 725 | } 726 | else 727 | { 728 | location.row = 0; 729 | location.col = 0; 730 | } 731 | TiXmlParsingData data( p, TabSize(), location.row, location.col ); 732 | location = data.Cursor(); 733 | 734 | if ( encoding == TIXML_ENCODING_UNKNOWN ) 735 | { 736 | // Check for the Microsoft UTF-8 lead bytes. 737 | const unsigned char* pU = (const unsigned char*)p; 738 | if ( *(pU+0) && *(pU+0) == TIXML_UTF_LEAD_0 739 | && *(pU+1) && *(pU+1) == TIXML_UTF_LEAD_1 740 | && *(pU+2) && *(pU+2) == TIXML_UTF_LEAD_2 ) 741 | { 742 | encoding = TIXML_ENCODING_UTF8; 743 | useMicrosoftBOM = true; 744 | } 745 | } 746 | 747 | p = SkipWhiteSpace( p, encoding ); 748 | if ( !p ) 749 | { 750 | SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN ); 751 | return 0; 752 | } 753 | 754 | while ( p && *p ) 755 | { 756 | TiXmlNode* node = Identify( p, encoding ); 757 | if ( node ) 758 | { 759 | p = node->Parse( p, &data, encoding ); 760 | LinkEndChild( node ); 761 | } 762 | else 763 | { 764 | break; 765 | } 766 | 767 | // Did we get encoding info? 768 | if ( encoding == TIXML_ENCODING_UNKNOWN 769 | && node->ToDeclaration() ) 770 | { 771 | TiXmlDeclaration* dec = node->ToDeclaration(); 772 | const char* enc = dec->Encoding(); 773 | assert( enc ); 774 | 775 | if ( *enc == 0 ) 776 | encoding = TIXML_ENCODING_UTF8; 777 | else if ( StringEqual( enc, "UTF-8", true, TIXML_ENCODING_UNKNOWN ) ) 778 | encoding = TIXML_ENCODING_UTF8; 779 | else if ( StringEqual( enc, "UTF8", true, TIXML_ENCODING_UNKNOWN ) ) 780 | encoding = TIXML_ENCODING_UTF8; // incorrect, but be nice 781 | else 782 | encoding = TIXML_ENCODING_LEGACY; 783 | } 784 | 785 | p = SkipWhiteSpace( p, encoding ); 786 | } 787 | 788 | // Was this empty? 789 | if ( !firstChild ) { 790 | SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, encoding ); 791 | return 0; 792 | } 793 | 794 | // All is well. 795 | return p; 796 | } 797 | 798 | void TiXmlDocument::SetError( int err, const char* pError, TiXmlParsingData* data, TiXmlEncoding encoding ) 799 | { 800 | // The first error in a chain is more accurate - don't set again! 801 | if ( error ) 802 | return; 803 | 804 | assert( err > 0 && err < TIXML_ERROR_STRING_COUNT ); 805 | error = true; 806 | errorId = err; 807 | errorDesc = errorString[ errorId ]; 808 | 809 | errorLocation.Clear(); 810 | if ( pError && data ) 811 | { 812 | data->Stamp( pError, encoding ); 813 | errorLocation = data->Cursor(); 814 | } 815 | } 816 | 817 | 818 | TiXmlNode* TiXmlNode::Identify( const char* p, TiXmlEncoding encoding ) 819 | { 820 | TiXmlNode* returnNode = 0; 821 | 822 | p = SkipWhiteSpace( p, encoding ); 823 | if( !p || !*p || *p != '<' ) 824 | { 825 | return 0; 826 | } 827 | 828 | p = SkipWhiteSpace( p, encoding ); 829 | 830 | if ( !p || !*p ) 831 | { 832 | return 0; 833 | } 834 | 835 | // What is this thing? 836 | // - Elements start with a letter or underscore, but xml is reserved. 837 | // - Comments: "; 1351 | 1352 | if ( !StringEqual( p, startTag, false, encoding ) ) 1353 | { 1354 | if ( document ) 1355 | document->SetError( TIXML_ERROR_PARSING_COMMENT, p, data, encoding ); 1356 | return 0; 1357 | } 1358 | p += strlen( startTag ); 1359 | 1360 | // [ 1475201 ] TinyXML parses entities in comments 1361 | // Oops - ReadText doesn't work, because we don't want to parse the entities. 1362 | // p = ReadText( p, &value, false, endTag, false, encoding ); 1363 | // 1364 | // from the XML spec: 1365 | /* 1366 | [Definition: Comments may appear anywhere in a document outside other markup; in addition, 1367 | they may appear within the document type declaration at places allowed by the grammar. 1368 | They are not part of the document's character data; an XML processor MAY, but need not, 1369 | make it possible for an application to retrieve the text of comments. For compatibility, 1370 | the string "--" (double-hyphen) MUST NOT occur within comments.] Parameter entity 1371 | references MUST NOT be recognized within comments. 1372 | 1373 | An example of a comment: 1374 | 1375 | 1376 | */ 1377 | 1378 | value = ""; 1379 | // Keep all the white space. 1380 | while ( p && *p && !StringEqual( p, endTag, false, encoding ) ) 1381 | { 1382 | value.append( p, 1 ); 1383 | ++p; 1384 | } 1385 | if ( p && *p ) 1386 | p += strlen( endTag ); 1387 | 1388 | return p; 1389 | } 1390 | 1391 | 1392 | const char* TiXmlAttribute::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding ) 1393 | { 1394 | p = SkipWhiteSpace( p, encoding ); 1395 | if ( !p || !*p ) return 0; 1396 | 1397 | if ( data ) 1398 | { 1399 | data->Stamp( p, encoding ); 1400 | location = data->Cursor(); 1401 | } 1402 | // Read the name, the '=' and the value. 1403 | const char* pErr = p; 1404 | p = ReadName( p, &name, encoding ); 1405 | if ( !p || !*p ) 1406 | { 1407 | if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, pErr, data, encoding ); 1408 | return 0; 1409 | } 1410 | p = SkipWhiteSpace( p, encoding ); 1411 | if ( !p || !*p || *p != '=' ) 1412 | { 1413 | if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding ); 1414 | return 0; 1415 | } 1416 | 1417 | ++p; // skip '=' 1418 | p = SkipWhiteSpace( p, encoding ); 1419 | if ( !p || !*p ) 1420 | { 1421 | if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding ); 1422 | return 0; 1423 | } 1424 | 1425 | const char* end; 1426 | const char SINGLE_QUOTE = '\''; 1427 | const char DOUBLE_QUOTE = '\"'; 1428 | 1429 | if ( *p == SINGLE_QUOTE ) 1430 | { 1431 | ++p; 1432 | end = "\'"; // single quote in string 1433 | p = ReadText( p, &value, false, end, false, encoding ); 1434 | } 1435 | else if ( *p == DOUBLE_QUOTE ) 1436 | { 1437 | ++p; 1438 | end = "\""; // double quote in string 1439 | p = ReadText( p, &value, false, end, false, encoding ); 1440 | } 1441 | else 1442 | { 1443 | // All attribute values should be in single or double quotes. 1444 | // But this is such a common error that the parser will try 1445 | // its best, even without them. 1446 | value = ""; 1447 | while ( p && *p // existence 1448 | && !IsWhiteSpace( *p ) // whitespace 1449 | && *p != '/' && *p != '>' ) // tag end 1450 | { 1451 | if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) { 1452 | // [ 1451649 ] Attribute values with trailing quotes not handled correctly 1453 | // We did not have an opening quote but seem to have a 1454 | // closing one. Give up and throw an error. 1455 | if ( document ) document->SetError( TIXML_ERROR_READING_ATTRIBUTES, p, data, encoding ); 1456 | return 0; 1457 | } 1458 | value += *p; 1459 | ++p; 1460 | } 1461 | } 1462 | return p; 1463 | } 1464 | 1465 | #ifdef TIXML_USE_STL 1466 | void TiXmlText::StreamIn( std::istream * in, TIXML_STRING * tag ) 1467 | { 1468 | while ( in->good() ) 1469 | { 1470 | int c = in->peek(); 1471 | if ( !cdata && (c == '<' ) ) 1472 | { 1473 | return; 1474 | } 1475 | if ( c <= 0 ) 1476 | { 1477 | TiXmlDocument* document = GetDocument(); 1478 | if ( document ) 1479 | document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN ); 1480 | return; 1481 | } 1482 | 1483 | (*tag) += (char) c; 1484 | in->get(); // "commits" the peek made above 1485 | 1486 | if ( cdata && c == '>' && tag->size() >= 3 ) { 1487 | size_t len = tag->size(); 1488 | if ( (*tag)[len-2] == ']' && (*tag)[len-3] == ']' ) { 1489 | // terminator of cdata. 1490 | return; 1491 | } 1492 | } 1493 | } 1494 | } 1495 | #endif 1496 | 1497 | const char* TiXmlText::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding ) 1498 | { 1499 | value = ""; 1500 | TiXmlDocument* document = GetDocument(); 1501 | 1502 | if ( data ) 1503 | { 1504 | data->Stamp( p, encoding ); 1505 | location = data->Cursor(); 1506 | } 1507 | 1508 | const char* const startTag = ""; 1510 | 1511 | if ( cdata || StringEqual( p, startTag, false, encoding ) ) 1512 | { 1513 | cdata = true; 1514 | 1515 | if ( !StringEqual( p, startTag, false, encoding ) ) 1516 | { 1517 | if ( document ) 1518 | document->SetError( TIXML_ERROR_PARSING_CDATA, p, data, encoding ); 1519 | return 0; 1520 | } 1521 | p += strlen( startTag ); 1522 | 1523 | // Keep all the white space, ignore the encoding, etc. 1524 | while ( p && *p 1525 | && !StringEqual( p, endTag, false, encoding ) 1526 | ) 1527 | { 1528 | value += *p; 1529 | ++p; 1530 | } 1531 | 1532 | TIXML_STRING dummy; 1533 | p = ReadText( p, &dummy, false, endTag, false, encoding ); 1534 | return p; 1535 | } 1536 | else 1537 | { 1538 | bool ignoreWhite = true; 1539 | 1540 | const char* end = "<"; 1541 | p = ReadText( p, &value, ignoreWhite, end, false, encoding ); 1542 | if ( p && *p ) 1543 | return p-1; // don't truncate the '<' 1544 | return 0; 1545 | } 1546 | } 1547 | 1548 | #ifdef TIXML_USE_STL 1549 | void TiXmlDeclaration::StreamIn( std::istream * in, TIXML_STRING * tag ) 1550 | { 1551 | while ( in->good() ) 1552 | { 1553 | int c = in->get(); 1554 | if ( c <= 0 ) 1555 | { 1556 | TiXmlDocument* document = GetDocument(); 1557 | if ( document ) 1558 | document->SetError( TIXML_ERROR_EMBEDDED_NULL, 0, 0, TIXML_ENCODING_UNKNOWN ); 1559 | return; 1560 | } 1561 | (*tag) += (char) c; 1562 | 1563 | if ( c == '>' ) 1564 | { 1565 | // All is well. 1566 | return; 1567 | } 1568 | } 1569 | } 1570 | #endif 1571 | 1572 | const char* TiXmlDeclaration::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding _encoding ) 1573 | { 1574 | p = SkipWhiteSpace( p, _encoding ); 1575 | // Find the beginning, find the end, and look for 1576 | // the stuff in-between. 1577 | TiXmlDocument* document = GetDocument(); 1578 | if ( !p || !*p || !StringEqual( p, "SetError( TIXML_ERROR_PARSING_DECLARATION, 0, 0, _encoding ); 1581 | return 0; 1582 | } 1583 | if ( data ) 1584 | { 1585 | data->Stamp( p, _encoding ); 1586 | location = data->Cursor(); 1587 | } 1588 | p += 5; 1589 | 1590 | version = ""; 1591 | encoding = ""; 1592 | standalone = ""; 1593 | 1594 | while ( p && *p ) 1595 | { 1596 | if ( *p == '>' ) 1597 | { 1598 | ++p; 1599 | return p; 1600 | } 1601 | 1602 | p = SkipWhiteSpace( p, _encoding ); 1603 | if ( StringEqual( p, "version", true, _encoding ) ) 1604 | { 1605 | TiXmlAttribute attrib; 1606 | p = attrib.Parse( p, data, _encoding ); 1607 | version = attrib.Value(); 1608 | } 1609 | else if ( StringEqual( p, "encoding", true, _encoding ) ) 1610 | { 1611 | TiXmlAttribute attrib; 1612 | p = attrib.Parse( p, data, _encoding ); 1613 | encoding = attrib.Value(); 1614 | } 1615 | else if ( StringEqual( p, "standalone", true, _encoding ) ) 1616 | { 1617 | TiXmlAttribute attrib; 1618 | p = attrib.Parse( p, data, _encoding ); 1619 | standalone = attrib.Value(); 1620 | } 1621 | else 1622 | { 1623 | // Read over whatever it is. 1624 | while( p && *p && *p != '>' && !IsWhiteSpace( *p ) ) 1625 | ++p; 1626 | } 1627 | } 1628 | return 0; 1629 | } 1630 | 1631 | bool TiXmlText::Blank() const 1632 | { 1633 | for ( unsigned i=0; i 2 | 3 | The world has many languages 4 | Мир имеет много языков 5 | el mundo tiene muchos idiomas 6 | 世界有很多语言 7 | <Русский название="name" ценность="value"><имеет> 8 | <汉语 名字="name" 价值="value">世界有很多语言 9 | "Mëtæl!" 10 | <ä>Umlaut Element 11 | 12 | -------------------------------------------------------------------------------- /SDL_snake/tinyxml/utf8testverify.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | The world has many languages 4 | Мир имеет много языков 5 | el mundo tiene muchos idiomas 6 | 世界有很多语言 7 | <Русский название="name" ценность="value"><имеет> 8 | <汉语 名字="name" 价值="value">世界有很多语言 9 | "Mëtæl!" 10 | <ä>Umlaut Element 11 | 12 | -------------------------------------------------------------------------------- /SDL_snake/tinyxml/xmltest.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/tinyxml/xmltest.cpp -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/DLL_FAQ.txt: -------------------------------------------------------------------------------- 1 | 2 | Frequently Asked Questions about ZLIB1.DLL 3 | 4 | 5 | This document describes the design, the rationale, and the usage 6 | of the official DLL build of zlib, named ZLIB1.DLL. If you have 7 | general questions about zlib, you should see the file "FAQ" found 8 | in the zlib distribution, or at the following location: 9 | http://www.gzip.org/zlib/zlib_faq.html 10 | 11 | 12 | 1. What is ZLIB1.DLL, and how can I get it? 13 | 14 | - ZLIB1.DLL is the official build of zlib as a DLL. 15 | (Please remark the character '1' in the name.) 16 | 17 | Pointers to a precompiled ZLIB1.DLL can be found in the zlib 18 | web site at: 19 | http://www.zlib.net/ 20 | 21 | Applications that link to ZLIB1.DLL can rely on the following 22 | specification: 23 | 24 | * The exported symbols are exclusively defined in the source 25 | files "zlib.h" and "zlib.def", found in an official zlib 26 | source distribution. 27 | * The symbols are exported by name, not by ordinal. 28 | * The exported names are undecorated. 29 | * The calling convention of functions is "C" (CDECL). 30 | * The ZLIB1.DLL binary is linked to MSVCRT.DLL. 31 | 32 | The archive in which ZLIB1.DLL is bundled contains compiled 33 | test programs that must run with a valid build of ZLIB1.DLL. 34 | It is recommended to download the prebuilt DLL from the zlib 35 | web site, instead of building it yourself, to avoid potential 36 | incompatibilities that could be introduced by your compiler 37 | and build settings. If you do build the DLL yourself, please 38 | make sure that it complies with all the above requirements, 39 | and it runs with the precompiled test programs, bundled with 40 | the original ZLIB1.DLL distribution. 41 | 42 | If, for any reason, you need to build an incompatible DLL, 43 | please use a different file name. 44 | 45 | 46 | 2. Why did you change the name of the DLL to ZLIB1.DLL? 47 | What happened to the old ZLIB.DLL? 48 | 49 | - The old ZLIB.DLL, built from zlib-1.1.4 or earlier, required 50 | compilation settings that were incompatible to those used by 51 | a static build. The DLL settings were supposed to be enabled 52 | by defining the macro ZLIB_DLL, before including "zlib.h". 53 | Incorrect handling of this macro was silently accepted at 54 | build time, resulting in two major problems: 55 | 56 | * ZLIB_DLL was missing from the old makefile. When building 57 | the DLL, not all people added it to the build options. In 58 | consequence, incompatible incarnations of ZLIB.DLL started 59 | to circulate around the net. 60 | 61 | * When switching from using the static library to using the 62 | DLL, applications had to define the ZLIB_DLL macro and 63 | to recompile all the sources that contained calls to zlib 64 | functions. Failure to do so resulted in creating binaries 65 | that were unable to run with the official ZLIB.DLL build. 66 | 67 | The only possible solution that we could foresee was to make 68 | a binary-incompatible change in the DLL interface, in order to 69 | remove the dependency on the ZLIB_DLL macro, and to release 70 | the new DLL under a different name. 71 | 72 | We chose the name ZLIB1.DLL, where '1' indicates the major 73 | zlib version number. We hope that we will not have to break 74 | the binary compatibility again, at least not as long as the 75 | zlib-1.x series will last. 76 | 77 | There is still a ZLIB_DLL macro, that can trigger a more 78 | efficient build and use of the DLL, but compatibility no 79 | longer dependents on it. 80 | 81 | 82 | 3. Can I build ZLIB.DLL from the new zlib sources, and replace 83 | an old ZLIB.DLL, that was built from zlib-1.1.4 or earlier? 84 | 85 | - In principle, you can do it by assigning calling convention 86 | keywords to the macros ZEXPORT and ZEXPORTVA. In practice, 87 | it depends on what you mean by "an old ZLIB.DLL", because the 88 | old DLL exists in several mutually-incompatible versions. 89 | You have to find out first what kind of calling convention is 90 | being used in your particular ZLIB.DLL build, and to use the 91 | same one in the new build. If you don't know what this is all 92 | about, you might be better off if you would just leave the old 93 | DLL intact. 94 | 95 | 96 | 4. Can I compile my application using the new zlib interface, and 97 | link it to an old ZLIB.DLL, that was built from zlib-1.1.4 or 98 | earlier? 99 | 100 | - The official answer is "no"; the real answer depends again on 101 | what kind of ZLIB.DLL you have. Even if you are lucky, this 102 | course of action is unreliable. 103 | 104 | If you rebuild your application and you intend to use a newer 105 | version of zlib (post- 1.1.4), it is strongly recommended to 106 | link it to the new ZLIB1.DLL. 107 | 108 | 109 | 5. Why are the zlib symbols exported by name, and not by ordinal? 110 | 111 | - Although exporting symbols by ordinal is a little faster, it 112 | is risky. Any single glitch in the maintenance or use of the 113 | DEF file that contains the ordinals can result in incompatible 114 | builds and frustrating crashes. Simply put, the benefits of 115 | exporting symbols by ordinal do not justify the risks. 116 | 117 | Technically, it should be possible to maintain ordinals in 118 | the DEF file, and still export the symbols by name. Ordinals 119 | exist in every DLL, and even if the dynamic linking performed 120 | at the DLL startup is searching for names, ordinals serve as 121 | hints, for a faster name lookup. However, if the DEF file 122 | contains ordinals, the Microsoft linker automatically builds 123 | an implib that will cause the executables linked to it to use 124 | those ordinals, and not the names. It is interesting to 125 | notice that the GNU linker for Win32 does not suffer from this 126 | problem. 127 | 128 | It is possible to avoid the DEF file if the exported symbols 129 | are accompanied by a "__declspec(dllexport)" attribute in the 130 | source files. You can do this in zlib by predefining the 131 | ZLIB_DLL macro. 132 | 133 | 134 | 6. I see that the ZLIB1.DLL functions use the "C" (CDECL) calling 135 | convention. Why not use the STDCALL convention? 136 | STDCALL is the standard convention in Win32, and I need it in 137 | my Visual Basic project! 138 | 139 | (For readability, we use CDECL to refer to the convention 140 | triggered by the "__cdecl" keyword, STDCALL to refer to 141 | the convention triggered by "__stdcall", and FASTCALL to 142 | refer to the convention triggered by "__fastcall".) 143 | 144 | - Most of the native Windows API functions (without varargs) use 145 | indeed the WINAPI convention (which translates to STDCALL in 146 | Win32), but the standard C functions use CDECL. If a user 147 | application is intrinsically tied to the Windows API (e.g. 148 | it calls native Windows API functions such as CreateFile()), 149 | sometimes it makes sense to decorate its own functions with 150 | WINAPI. But if ANSI C or POSIX portability is a goal (e.g. 151 | it calls standard C functions such as fopen()), it is not a 152 | sound decision to request the inclusion of , or to 153 | use non-ANSI constructs, for the sole purpose to make the user 154 | functions STDCALL-able. 155 | 156 | The functionality offered by zlib is not in the category of 157 | "Windows functionality", but is more like "C functionality". 158 | 159 | Technically, STDCALL is not bad; in fact, it is slightly 160 | faster than CDECL, and it works with variable-argument 161 | functions, just like CDECL. It is unfortunate that, in spite 162 | of using STDCALL in the Windows API, it is not the default 163 | convention used by the C compilers that run under Windows. 164 | The roots of the problem reside deep inside the unsafety of 165 | the K&R-style function prototypes, where the argument types 166 | are not specified; but that is another story for another day. 167 | 168 | The remaining fact is that CDECL is the default convention. 169 | Even if an explicit convention is hard-coded into the function 170 | prototypes inside C headers, problems may appear. The 171 | necessity to expose the convention in users' callbacks is one 172 | of these problems. 173 | 174 | The calling convention issues are also important when using 175 | zlib in other programming languages. Some of them, like Ada 176 | (GNAT) and Fortran (GNU G77), have C bindings implemented 177 | initially on Unix, and relying on the C calling convention. 178 | On the other hand, the pre- .NET versions of Microsoft Visual 179 | Basic require STDCALL, while Borland Delphi prefers, although 180 | it does not require, FASTCALL. 181 | 182 | In fairness to all possible uses of zlib outside the C 183 | programming language, we choose the default "C" convention. 184 | Anyone interested in different bindings or conventions is 185 | encouraged to maintain specialized projects. The "contrib/" 186 | directory from the zlib distribution already holds a couple 187 | of foreign bindings, such as Ada, C++, and Delphi. 188 | 189 | 190 | 7. I need a DLL for my Visual Basic project. What can I do? 191 | 192 | - Define the ZLIB_WINAPI macro before including "zlib.h", when 193 | building both the DLL and the user application (except that 194 | you don't need to define anything when using the DLL in Visual 195 | Basic). The ZLIB_WINAPI macro will switch on the WINAPI 196 | (STDCALL) convention. The name of this DLL must be different 197 | than the official ZLIB1.DLL. 198 | 199 | Gilles Vollant has contributed a build named ZLIBWAPI.DLL, 200 | with the ZLIB_WINAPI macro turned on, and with the minizip 201 | functionality built in. For more information, please read 202 | the notes inside "contrib/vstudio/readme.txt", found in the 203 | zlib distribution. 204 | 205 | 206 | 8. I need to use zlib in my Microsoft .NET project. What can I 207 | do? 208 | 209 | - Henrik Ravn has contributed a .NET wrapper around zlib. Look 210 | into contrib/dotzlib/, inside the zlib distribution. 211 | 212 | 213 | 9. If my application uses ZLIB1.DLL, should I link it to 214 | MSVCRT.DLL? Why? 215 | 216 | - It is not required, but it is recommended to link your 217 | application to MSVCRT.DLL, if it uses ZLIB1.DLL. 218 | 219 | The executables (.EXE, .DLL, etc.) that are involved in the 220 | same process and are using the C run-time library (i.e. they 221 | are calling standard C functions), must link to the same 222 | library. There are several libraries in the Win32 system: 223 | CRTDLL.DLL, MSVCRT.DLL, the static C libraries, etc. 224 | Since ZLIB1.DLL is linked to MSVCRT.DLL, the executables that 225 | depend on it should also be linked to MSVCRT.DLL. 226 | 227 | 228 | 10. Why are you saying that ZLIB1.DLL and my application should 229 | be linked to the same C run-time (CRT) library? I linked my 230 | application and my DLLs to different C libraries (e.g. my 231 | application to a static library, and my DLLs to MSVCRT.DLL), 232 | and everything works fine. 233 | 234 | - If a user library invokes only pure Win32 API (accessible via 235 | and the related headers), its DLL build will work 236 | in any context. But if this library invokes standard C API, 237 | things get more complicated. 238 | 239 | There is a single Win32 library in a Win32 system. Every 240 | function in this library resides in a single DLL module, that 241 | is safe to call from anywhere. On the other hand, there are 242 | multiple versions of the C library, and each of them has its 243 | own separate internal state. Standalone executables and user 244 | DLLs that call standard C functions must link to a C run-time 245 | (CRT) library, be it static or shared (DLL). Intermixing 246 | occurs when an executable (not necessarily standalone) and a 247 | DLL are linked to different CRTs, and both are running in the 248 | same process. 249 | 250 | Intermixing multiple CRTs is possible, as long as their 251 | internal states are kept intact. The Microsoft Knowledge Base 252 | articles KB94248 "HOWTO: Use the C Run-Time" and KB140584 253 | "HOWTO: Link with the Correct C Run-Time (CRT) Library" 254 | mention the potential problems raised by intermixing. 255 | 256 | If intermixing works for you, it's because your application 257 | and DLLs are avoiding the corruption of each of the CRTs' 258 | internal states, maybe by careful design, or maybe by fortune. 259 | 260 | Also note that linking ZLIB1.DLL to non-Microsoft CRTs, such 261 | as those provided by Borland, raises similar problems. 262 | 263 | 264 | 11. Why are you linking ZLIB1.DLL to MSVCRT.DLL? 265 | 266 | - MSVCRT.DLL exists on every Windows 95 with a new service pack 267 | installed, or with Microsoft Internet Explorer 4 or later, and 268 | on all other Windows 4.x or later (Windows 98, Windows NT 4, 269 | or later). It is freely distributable; if not present in the 270 | system, it can be downloaded from Microsoft or from other 271 | software provider for free. 272 | 273 | The fact that MSVCRT.DLL does not exist on a virgin Windows 95 274 | is not so problematic. Windows 95 is scarcely found nowadays, 275 | Microsoft ended its support a long time ago, and many recent 276 | applications from various vendors, including Microsoft, do not 277 | even run on it. Furthermore, no serious user should run 278 | Windows 95 without a proper update installed. 279 | 280 | 281 | 12. Why are you not linking ZLIB1.DLL to 282 | <> ? 283 | 284 | - We considered and abandoned the following alternatives: 285 | 286 | * Linking ZLIB1.DLL to a static C library (LIBC.LIB, or 287 | LIBCMT.LIB) is not a good option. People are using the DLL 288 | mainly to save disk space. If you are linking your program 289 | to a static C library, you may as well consider linking zlib 290 | in statically, too. 291 | 292 | * Linking ZLIB1.DLL to CRTDLL.DLL looks appealing, because 293 | CRTDLL.DLL is present on every Win32 installation. 294 | Unfortunately, it has a series of problems: it does not 295 | work properly with Microsoft's C++ libraries, it does not 296 | provide support for 64-bit file offsets, (and so on...), 297 | and Microsoft discontinued its support a long time ago. 298 | 299 | * Linking ZLIB1.DLL to MSVCR70.DLL or MSVCR71.DLL, supplied 300 | with the Microsoft .NET platform, and Visual C++ 7.0/7.1, 301 | raises problems related to the status of ZLIB1.DLL as a 302 | system component. According to the Microsoft Knowledge Base 303 | article KB326922 "INFO: Redistribution of the Shared C 304 | Runtime Component in Visual C++ .NET", MSVCR70.DLL and 305 | MSVCR71.DLL are not supposed to function as system DLLs, 306 | because they may clash with MSVCRT.DLL. Instead, the 307 | application's installer is supposed to put these DLLs 308 | (if needed) in the application's private directory. 309 | If ZLIB1.DLL depends on a non-system runtime, it cannot 310 | function as a redistributable system component. 311 | 312 | * Linking ZLIB1.DLL to non-Microsoft runtimes, such as 313 | Borland's, or Cygwin's, raises problems related to the 314 | reliable presence of these runtimes on Win32 systems. 315 | It's easier to let the DLL build of zlib up to the people 316 | who distribute these runtimes, and who may proceed as 317 | explained in the answer to Question 14. 318 | 319 | 320 | 13. If ZLIB1.DLL cannot be linked to MSVCR70.DLL or MSVCR71.DLL, 321 | how can I build/use ZLIB1.DLL in Microsoft Visual C++ 7.0 322 | (Visual Studio .NET) or newer? 323 | 324 | - Due to the problems explained in the Microsoft Knowledge Base 325 | article KB326922 (see the previous answer), the C runtime that 326 | comes with the VC7 environment is no longer considered a 327 | system component. That is, it should not be assumed that this 328 | runtime exists, or may be installed in a system directory. 329 | Since ZLIB1.DLL is supposed to be a system component, it may 330 | not depend on a non-system component. 331 | 332 | In order to link ZLIB1.DLL and your application to MSVCRT.DLL 333 | in VC7, you need the library of Visual C++ 6.0 or older. If 334 | you don't have this library at hand, it's probably best not to 335 | use ZLIB1.DLL. 336 | 337 | We are hoping that, in the future, Microsoft will provide a 338 | way to build applications linked to a proper system runtime, 339 | from the Visual C++ environment. Until then, you have a 340 | couple of alternatives, such as linking zlib in statically. 341 | If your application requires dynamic linking, you may proceed 342 | as explained in the answer to Question 14. 343 | 344 | 345 | 14. I need to link my own DLL build to a CRT different than 346 | MSVCRT.DLL. What can I do? 347 | 348 | - Feel free to rebuild the DLL from the zlib sources, and link 349 | it the way you want. You should, however, clearly state that 350 | your build is unofficial. You should give it a different file 351 | name, and/or install it in a private directory that can be 352 | accessed by your application only, and is not visible to the 353 | others (i.e. it's neither in the PATH, nor in the SYSTEM or 354 | SYSTEM32 directories). Otherwise, your build may clash with 355 | applications that link to the official build. 356 | 357 | For example, in Cygwin, zlib is linked to the Cygwin runtime 358 | CYGWIN1.DLL, and it is distributed under the name CYGZ.DLL. 359 | 360 | 361 | 15. May I include additional pieces of code that I find useful, 362 | link them in ZLIB1.DLL, and export them? 363 | 364 | - No. A legitimate build of ZLIB1.DLL must not include code 365 | that does not originate from the official zlib source code. 366 | But you can make your own private DLL build, under a different 367 | file name, as suggested in the previous answer. 368 | 369 | For example, zlib is a part of the VCL library, distributed 370 | with Borland Delphi and C++ Builder. The DLL build of VCL 371 | is a redistributable file, named VCLxx.DLL. 372 | 373 | 374 | 16. May I remove some functionality out of ZLIB1.DLL, by enabling 375 | macros like NO_GZCOMPRESS or NO_GZIP at compile time? 376 | 377 | - No. A legitimate build of ZLIB1.DLL must provide the complete 378 | zlib functionality, as implemented in the official zlib source 379 | code. But you can make your own private DLL build, under a 380 | different file name, as suggested in the previous answer. 381 | 382 | 383 | 17. I made my own ZLIB1.DLL build. Can I test it for compliance? 384 | 385 | - We prefer that you download the official DLL from the zlib 386 | web site. If you need something peculiar from this DLL, you 387 | can send your suggestion to the zlib mailing list. 388 | 389 | However, in case you do rebuild the DLL yourself, you can run 390 | it with the test programs found in the DLL distribution. 391 | Running these test programs is not a guarantee of compliance, 392 | but a failure can imply a detected problem. 393 | 394 | ** 395 | 396 | This document is written and maintained by 397 | Cosmin Truta 398 | -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/README.txt: -------------------------------------------------------------------------------- 1 | 2 | What's here 3 | =========== 4 | The official ZLIB1.DLL 5 | 6 | 7 | Source 8 | ====== 9 | zlib version 1.2.8 10 | available at http://www.gzip.org/zlib/ 11 | 12 | 13 | Specification and rationale 14 | =========================== 15 | See the accompanying DLL_FAQ.txt 16 | 17 | 18 | Usage 19 | ===== 20 | See the accompanying USAGE.txt 21 | 22 | 23 | Build info 24 | ========== 25 | Contributed by Jan Nijtmans. 26 | 27 | Compiler: 28 | i686-w64-mingw32-gcc (GCC) 4.5.3 29 | Library: 30 | mingw64-i686-runtime/headers: 3.0b_svn5747-1 31 | Build commands: 32 | i686-w64-mingw32-gcc -c -DASMV contrib/asm686/match.S 33 | i686-w64-mingw32-gcc -c -DASMINF -I. -O3 contrib/inflate86/inffas86.c 34 | make -f win32/Makefile.gcc PREFIX=i686-w64-mingw32- LOC="-mms-bitfields -DASMV -DASMINF" OBJA="inffas86.o match.o" 35 | Finally, from VS commandline (VS2005 or higher): 36 | lib -machine:X86 -name:zlib1.dll -def:zlib.def -out:zdll.lib 37 | 38 | Copyright notice 39 | ================ 40 | Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler 41 | 42 | This software is provided 'as-is', without any express or implied 43 | warranty. In no event will the authors be held liable for any damages 44 | arising from the use of this software. 45 | 46 | Permission is granted to anyone to use this software for any purpose, 47 | including commercial applications, and to alter it and redistribute it 48 | freely, subject to the following restrictions: 49 | 50 | 1. The origin of this software must not be misrepresented; you must not 51 | claim that you wrote the original software. If you use this software 52 | in a product, an acknowledgment in the product documentation would be 53 | appreciated but is not required. 54 | 2. Altered source versions must be plainly marked as such, and must not be 55 | misrepresented as being the original software. 56 | 3. This notice may not be removed or altered from any source distribution. 57 | 58 | Jean-loup Gailly Mark Adler 59 | jloup@gzip.org madler@alumni.caltech.edu 60 | 61 | -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/USAGE.txt: -------------------------------------------------------------------------------- 1 | 2 | Installing ZLIB1.DLL 3 | ==================== 4 | Copy ZLIB1.DLL to the SYSTEM or the SYSTEM32 directory. 5 | 6 | 7 | Using ZLIB1.DLL with Microsoft Visual C++ 8 | ========================================= 9 | 1. Install the supplied header files "zlib.h" and "zconf.h" 10 | into a directory found in the INCLUDE path list. 11 | 12 | 2. Install the supplied library file "zdll.lib" into a 13 | directory found in the LIB path list. 14 | 15 | 3. Add "zdll.lib" to your project. 16 | 17 | 18 | Using ZLIB1.DLL with gcc/MinGW 19 | ============================== 20 | 1. Install the supplied header files "zlib.h" and "zconf.h" 21 | into the INCLUDE directory. 22 | 23 | 2. Copy the supplied library file "zdll.lib" to "libzdll.a": 24 | cp lib/zdll.lib lib/libzdll.a 25 | 26 | OR 27 | 28 | 2' Build the import library from the supplied "zlib.def": 29 | dlltool -D zlib1.dll -d lib/zlib.def -l lib/libzdll.a 30 | 31 | 3. Install "libzdll.a" into the LIB directory. 32 | 33 | 4. Add "libzdll.a" to your project, or use the -lzdll option. 34 | 35 | 36 | Using ZLIB1.DLL with gcc/Cygwin 37 | =============================== 38 | ZLIB1.DLL is not designed to work with Cygwin. The Cygwin 39 | system has its own DLL build of zlib, named CYGZ.DLL. 40 | 41 | 42 | Using ZLIB1.DLL with Borland C++ 43 | ================================ 44 | 1. Install the supplied header files "zlib.h" and "zconf.h" 45 | into a directory found in the INCLUDE path list. 46 | 47 | 2. Build the import library using the IMPLIB tool: 48 | implib -a -c -f lib\zdllbor.lib zlib1.dll 49 | 50 | OR 51 | 52 | 2' Convert the supplied library file "zdll.lib" to OMF format, 53 | using the COFF2OMF tool: 54 | coff2omf lib\zdll.lib lib\zdllbor.lib 55 | 56 | 3. Install "zdllbor.lib" into a directory found in the LIB path 57 | list. 58 | 59 | 4. Add "zdllbor.lib" to your project. 60 | 61 | Notes: 62 | - The modules that are linked with "zdllbor.lib" must be compiled 63 | using a 4-byte alignment (option -a): 64 | bcc32 -a -c myprog.c 65 | bcc32 myprog.obj zdllbor.lib 66 | - If you wish, you may use "zlib1.lib" instead of "zdllbor.lib". 67 | 68 | 69 | Rebuilding ZLIB1.DLL 70 | ==================== 71 | Depending on your build environment, use the appropriate 72 | makefile from the win32/ directory, found in the zlib source 73 | distribution. 74 | 75 | Your custom build has to comply with the requirements stated 76 | in DLL_FAQ.txt, including (but not limited to) the following: 77 | - It must be built from an unaltered zlib source distribution. 78 | - It must be linked to MSVCRT.DLL. 79 | - The macros that compile out certain portions of the zlib 80 | code (such as NO_GZCOMPRESS, NO_GZIP) must not be enabled. 81 | - The ZLIB_WINAPI macro must not be enabled. 82 | 83 | Furthermore, it has to run successfully with the test suite 84 | found in this package. 85 | 86 | It is recommended, however, to use the supplied ZLIB1.DLL, 87 | instead of rebuilding it yourself. You should rebuild it 88 | only if you have a special reason. 89 | 90 | -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/include/zconf.h: -------------------------------------------------------------------------------- 1 | /* zconf.h -- configuration of the zlib compression library 2 | * Copyright (C) 1995-2013 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #ifndef ZCONF_H 9 | #define ZCONF_H 10 | 11 | /* 12 | * If you *really* need a unique prefix for all types and library functions, 13 | * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. 14 | * Even better than compiling with -DZ_PREFIX would be to use configure to set 15 | * this permanently in zconf.h using "./configure --zprefix". 16 | */ 17 | #ifdef Z_PREFIX /* may be set to #if 1 by ./configure */ 18 | # define Z_PREFIX_SET 19 | 20 | /* all linked symbols */ 21 | # define _dist_code z__dist_code 22 | # define _length_code z__length_code 23 | # define _tr_align z__tr_align 24 | # define _tr_flush_bits z__tr_flush_bits 25 | # define _tr_flush_block z__tr_flush_block 26 | # define _tr_init z__tr_init 27 | # define _tr_stored_block z__tr_stored_block 28 | # define _tr_tally z__tr_tally 29 | # define adler32 z_adler32 30 | # define adler32_combine z_adler32_combine 31 | # define adler32_combine64 z_adler32_combine64 32 | # ifndef Z_SOLO 33 | # define compress z_compress 34 | # define compress2 z_compress2 35 | # define compressBound z_compressBound 36 | # endif 37 | # define crc32 z_crc32 38 | # define crc32_combine z_crc32_combine 39 | # define crc32_combine64 z_crc32_combine64 40 | # define deflate z_deflate 41 | # define deflateBound z_deflateBound 42 | # define deflateCopy z_deflateCopy 43 | # define deflateEnd z_deflateEnd 44 | # define deflateInit2_ z_deflateInit2_ 45 | # define deflateInit_ z_deflateInit_ 46 | # define deflateParams z_deflateParams 47 | # define deflatePending z_deflatePending 48 | # define deflatePrime z_deflatePrime 49 | # define deflateReset z_deflateReset 50 | # define deflateResetKeep z_deflateResetKeep 51 | # define deflateSetDictionary z_deflateSetDictionary 52 | # define deflateSetHeader z_deflateSetHeader 53 | # define deflateTune z_deflateTune 54 | # define deflate_copyright z_deflate_copyright 55 | # define get_crc_table z_get_crc_table 56 | # ifndef Z_SOLO 57 | # define gz_error z_gz_error 58 | # define gz_intmax z_gz_intmax 59 | # define gz_strwinerror z_gz_strwinerror 60 | # define gzbuffer z_gzbuffer 61 | # define gzclearerr z_gzclearerr 62 | # define gzclose z_gzclose 63 | # define gzclose_r z_gzclose_r 64 | # define gzclose_w z_gzclose_w 65 | # define gzdirect z_gzdirect 66 | # define gzdopen z_gzdopen 67 | # define gzeof z_gzeof 68 | # define gzerror z_gzerror 69 | # define gzflush z_gzflush 70 | # define gzgetc z_gzgetc 71 | # define gzgetc_ z_gzgetc_ 72 | # define gzgets z_gzgets 73 | # define gzoffset z_gzoffset 74 | # define gzoffset64 z_gzoffset64 75 | # define gzopen z_gzopen 76 | # define gzopen64 z_gzopen64 77 | # ifdef _WIN32 78 | # define gzopen_w z_gzopen_w 79 | # endif 80 | # define gzprintf z_gzprintf 81 | # define gzvprintf z_gzvprintf 82 | # define gzputc z_gzputc 83 | # define gzputs z_gzputs 84 | # define gzread z_gzread 85 | # define gzrewind z_gzrewind 86 | # define gzseek z_gzseek 87 | # define gzseek64 z_gzseek64 88 | # define gzsetparams z_gzsetparams 89 | # define gztell z_gztell 90 | # define gztell64 z_gztell64 91 | # define gzungetc z_gzungetc 92 | # define gzwrite z_gzwrite 93 | # endif 94 | # define inflate z_inflate 95 | # define inflateBack z_inflateBack 96 | # define inflateBackEnd z_inflateBackEnd 97 | # define inflateBackInit_ z_inflateBackInit_ 98 | # define inflateCopy z_inflateCopy 99 | # define inflateEnd z_inflateEnd 100 | # define inflateGetHeader z_inflateGetHeader 101 | # define inflateInit2_ z_inflateInit2_ 102 | # define inflateInit_ z_inflateInit_ 103 | # define inflateMark z_inflateMark 104 | # define inflatePrime z_inflatePrime 105 | # define inflateReset z_inflateReset 106 | # define inflateReset2 z_inflateReset2 107 | # define inflateSetDictionary z_inflateSetDictionary 108 | # define inflateGetDictionary z_inflateGetDictionary 109 | # define inflateSync z_inflateSync 110 | # define inflateSyncPoint z_inflateSyncPoint 111 | # define inflateUndermine z_inflateUndermine 112 | # define inflateResetKeep z_inflateResetKeep 113 | # define inflate_copyright z_inflate_copyright 114 | # define inflate_fast z_inflate_fast 115 | # define inflate_table z_inflate_table 116 | # ifndef Z_SOLO 117 | # define uncompress z_uncompress 118 | # endif 119 | # define zError z_zError 120 | # ifndef Z_SOLO 121 | # define zcalloc z_zcalloc 122 | # define zcfree z_zcfree 123 | # endif 124 | # define zlibCompileFlags z_zlibCompileFlags 125 | # define zlibVersion z_zlibVersion 126 | 127 | /* all zlib typedefs in zlib.h and zconf.h */ 128 | # define Byte z_Byte 129 | # define Bytef z_Bytef 130 | # define alloc_func z_alloc_func 131 | # define charf z_charf 132 | # define free_func z_free_func 133 | # ifndef Z_SOLO 134 | # define gzFile z_gzFile 135 | # endif 136 | # define gz_header z_gz_header 137 | # define gz_headerp z_gz_headerp 138 | # define in_func z_in_func 139 | # define intf z_intf 140 | # define out_func z_out_func 141 | # define uInt z_uInt 142 | # define uIntf z_uIntf 143 | # define uLong z_uLong 144 | # define uLongf z_uLongf 145 | # define voidp z_voidp 146 | # define voidpc z_voidpc 147 | # define voidpf z_voidpf 148 | 149 | /* all zlib structs in zlib.h and zconf.h */ 150 | # define gz_header_s z_gz_header_s 151 | # define internal_state z_internal_state 152 | 153 | #endif 154 | 155 | #if defined(__MSDOS__) && !defined(MSDOS) 156 | # define MSDOS 157 | #endif 158 | #if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) 159 | # define OS2 160 | #endif 161 | #if defined(_WINDOWS) && !defined(WINDOWS) 162 | # define WINDOWS 163 | #endif 164 | #if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) 165 | # ifndef WIN32 166 | # define WIN32 167 | # endif 168 | #endif 169 | #if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) 170 | # if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) 171 | # ifndef SYS16BIT 172 | # define SYS16BIT 173 | # endif 174 | # endif 175 | #endif 176 | 177 | /* 178 | * Compile with -DMAXSEG_64K if the alloc function cannot allocate more 179 | * than 64k bytes at a time (needed on systems with 16-bit int). 180 | */ 181 | #ifdef SYS16BIT 182 | # define MAXSEG_64K 183 | #endif 184 | #ifdef MSDOS 185 | # define UNALIGNED_OK 186 | #endif 187 | 188 | #ifdef __STDC_VERSION__ 189 | # ifndef STDC 190 | # define STDC 191 | # endif 192 | # if __STDC_VERSION__ >= 199901L 193 | # ifndef STDC99 194 | # define STDC99 195 | # endif 196 | # endif 197 | #endif 198 | #if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) 199 | # define STDC 200 | #endif 201 | #if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) 202 | # define STDC 203 | #endif 204 | #if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) 205 | # define STDC 206 | #endif 207 | #if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) 208 | # define STDC 209 | #endif 210 | 211 | #if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ 212 | # define STDC 213 | #endif 214 | 215 | #ifndef STDC 216 | # ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ 217 | # define const /* note: need a more gentle solution here */ 218 | # endif 219 | #endif 220 | 221 | #if defined(ZLIB_CONST) && !defined(z_const) 222 | # define z_const const 223 | #else 224 | # define z_const 225 | #endif 226 | 227 | /* Some Mac compilers merge all .h files incorrectly: */ 228 | #if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) 229 | # define NO_DUMMY_DECL 230 | #endif 231 | 232 | /* Maximum value for memLevel in deflateInit2 */ 233 | #ifndef MAX_MEM_LEVEL 234 | # ifdef MAXSEG_64K 235 | # define MAX_MEM_LEVEL 8 236 | # else 237 | # define MAX_MEM_LEVEL 9 238 | # endif 239 | #endif 240 | 241 | /* Maximum value for windowBits in deflateInit2 and inflateInit2. 242 | * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files 243 | * created by gzip. (Files created by minigzip can still be extracted by 244 | * gzip.) 245 | */ 246 | #ifndef MAX_WBITS 247 | # define MAX_WBITS 15 /* 32K LZ77 window */ 248 | #endif 249 | 250 | /* The memory requirements for deflate are (in bytes): 251 | (1 << (windowBits+2)) + (1 << (memLevel+9)) 252 | that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) 253 | plus a few kilobytes for small objects. For example, if you want to reduce 254 | the default memory requirements from 256K to 128K, compile with 255 | make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" 256 | Of course this will generally degrade compression (there's no free lunch). 257 | 258 | The memory requirements for inflate are (in bytes) 1 << windowBits 259 | that is, 32K for windowBits=15 (default value) plus a few kilobytes 260 | for small objects. 261 | */ 262 | 263 | /* Type declarations */ 264 | 265 | #ifndef OF /* function prototypes */ 266 | # ifdef STDC 267 | # define OF(args) args 268 | # else 269 | # define OF(args) () 270 | # endif 271 | #endif 272 | 273 | #ifndef Z_ARG /* function prototypes for stdarg */ 274 | # if defined(STDC) || defined(Z_HAVE_STDARG_H) 275 | # define Z_ARG(args) args 276 | # else 277 | # define Z_ARG(args) () 278 | # endif 279 | #endif 280 | 281 | /* The following definitions for FAR are needed only for MSDOS mixed 282 | * model programming (small or medium model with some far allocations). 283 | * This was tested only with MSC; for other MSDOS compilers you may have 284 | * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, 285 | * just define FAR to be empty. 286 | */ 287 | #ifdef SYS16BIT 288 | # if defined(M_I86SM) || defined(M_I86MM) 289 | /* MSC small or medium model */ 290 | # define SMALL_MEDIUM 291 | # ifdef _MSC_VER 292 | # define FAR _far 293 | # else 294 | # define FAR far 295 | # endif 296 | # endif 297 | # if (defined(__SMALL__) || defined(__MEDIUM__)) 298 | /* Turbo C small or medium model */ 299 | # define SMALL_MEDIUM 300 | # ifdef __BORLANDC__ 301 | # define FAR _far 302 | # else 303 | # define FAR far 304 | # endif 305 | # endif 306 | #endif 307 | 308 | #if defined(WINDOWS) || defined(WIN32) 309 | /* If building or using zlib as a DLL, define ZLIB_DLL. 310 | * This is not mandatory, but it offers a little performance increase. 311 | */ 312 | # ifdef ZLIB_DLL 313 | # if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) 314 | # ifdef ZLIB_INTERNAL 315 | # define ZEXTERN extern __declspec(dllexport) 316 | # else 317 | # define ZEXTERN extern __declspec(dllimport) 318 | # endif 319 | # endif 320 | # endif /* ZLIB_DLL */ 321 | /* If building or using zlib with the WINAPI/WINAPIV calling convention, 322 | * define ZLIB_WINAPI. 323 | * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. 324 | */ 325 | # ifdef ZLIB_WINAPI 326 | # ifdef FAR 327 | # undef FAR 328 | # endif 329 | # include 330 | /* No need for _export, use ZLIB.DEF instead. */ 331 | /* For complete Windows compatibility, use WINAPI, not __stdcall. */ 332 | # define ZEXPORT WINAPI 333 | # ifdef WIN32 334 | # define ZEXPORTVA WINAPIV 335 | # else 336 | # define ZEXPORTVA FAR CDECL 337 | # endif 338 | # endif 339 | #endif 340 | 341 | #if defined (__BEOS__) 342 | # ifdef ZLIB_DLL 343 | # ifdef ZLIB_INTERNAL 344 | # define ZEXPORT __declspec(dllexport) 345 | # define ZEXPORTVA __declspec(dllexport) 346 | # else 347 | # define ZEXPORT __declspec(dllimport) 348 | # define ZEXPORTVA __declspec(dllimport) 349 | # endif 350 | # endif 351 | #endif 352 | 353 | #ifndef ZEXTERN 354 | # define ZEXTERN extern 355 | #endif 356 | #ifndef ZEXPORT 357 | # define ZEXPORT 358 | #endif 359 | #ifndef ZEXPORTVA 360 | # define ZEXPORTVA 361 | #endif 362 | 363 | #ifndef FAR 364 | # define FAR 365 | #endif 366 | 367 | #if !defined(__MACTYPES__) 368 | typedef unsigned char Byte; /* 8 bits */ 369 | #endif 370 | typedef unsigned int uInt; /* 16 bits or more */ 371 | typedef unsigned long uLong; /* 32 bits or more */ 372 | 373 | #ifdef SMALL_MEDIUM 374 | /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ 375 | # define Bytef Byte FAR 376 | #else 377 | typedef Byte FAR Bytef; 378 | #endif 379 | typedef char FAR charf; 380 | typedef int FAR intf; 381 | typedef uInt FAR uIntf; 382 | typedef uLong FAR uLongf; 383 | 384 | #ifdef STDC 385 | typedef void const *voidpc; 386 | typedef void FAR *voidpf; 387 | typedef void *voidp; 388 | #else 389 | typedef Byte const *voidpc; 390 | typedef Byte FAR *voidpf; 391 | typedef Byte *voidp; 392 | #endif 393 | 394 | #if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) 395 | # include 396 | # if (UINT_MAX == 0xffffffffUL) 397 | # define Z_U4 unsigned 398 | # elif (ULONG_MAX == 0xffffffffUL) 399 | # define Z_U4 unsigned long 400 | # elif (USHRT_MAX == 0xffffffffUL) 401 | # define Z_U4 unsigned short 402 | # endif 403 | #endif 404 | 405 | #ifdef Z_U4 406 | typedef Z_U4 z_crc_t; 407 | #else 408 | typedef unsigned long z_crc_t; 409 | #endif 410 | 411 | #ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ 412 | # define Z_HAVE_UNISTD_H 413 | #endif 414 | 415 | #ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */ 416 | # define Z_HAVE_STDARG_H 417 | #endif 418 | 419 | #ifdef STDC 420 | # ifndef Z_SOLO 421 | # include /* for off_t */ 422 | # endif 423 | #endif 424 | 425 | #if defined(STDC) || defined(Z_HAVE_STDARG_H) 426 | # ifndef Z_SOLO 427 | # include /* for va_list */ 428 | # endif 429 | #endif 430 | 431 | #ifdef _WIN32 432 | # ifndef Z_SOLO 433 | # include /* for wchar_t */ 434 | # endif 435 | #endif 436 | 437 | /* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and 438 | * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even 439 | * though the former does not conform to the LFS document), but considering 440 | * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as 441 | * equivalently requesting no 64-bit operations 442 | */ 443 | #if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1 444 | # undef _LARGEFILE64_SOURCE 445 | #endif 446 | 447 | #if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H) 448 | # define Z_HAVE_UNISTD_H 449 | #endif 450 | #ifndef Z_SOLO 451 | # if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE) 452 | # include /* for SEEK_*, off_t, and _LFS64_LARGEFILE */ 453 | # ifdef VMS 454 | # include /* for off_t */ 455 | # endif 456 | # ifndef z_off_t 457 | # define z_off_t off_t 458 | # endif 459 | # endif 460 | #endif 461 | 462 | #if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0 463 | # define Z_LFS64 464 | #endif 465 | 466 | #if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) 467 | # define Z_LARGE64 468 | #endif 469 | 470 | #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64) 471 | # define Z_WANT64 472 | #endif 473 | 474 | #if !defined(SEEK_SET) && !defined(Z_SOLO) 475 | # define SEEK_SET 0 /* Seek from beginning of file. */ 476 | # define SEEK_CUR 1 /* Seek from current position. */ 477 | # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ 478 | #endif 479 | 480 | #ifndef z_off_t 481 | # define z_off_t long 482 | #endif 483 | 484 | #if !defined(_WIN32) && defined(Z_LARGE64) 485 | # define z_off64_t off64_t 486 | #else 487 | # if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO) 488 | # define z_off64_t __int64 489 | # else 490 | # define z_off64_t z_off_t 491 | # endif 492 | #endif 493 | 494 | /* MVS linker does not support external names larger than 8 bytes */ 495 | #if defined(__MVS__) 496 | #pragma map(deflateInit_,"DEIN") 497 | #pragma map(deflateInit2_,"DEIN2") 498 | #pragma map(deflateEnd,"DEEND") 499 | #pragma map(deflateBound,"DEBND") 500 | #pragma map(inflateInit_,"ININ") 501 | #pragma map(inflateInit2_,"ININ2") 502 | #pragma map(inflateEnd,"INEND") 503 | #pragma map(inflateSync,"INSY") 504 | #pragma map(inflateSetDictionary,"INSEDI") 505 | #pragma map(compressBound,"CMBND") 506 | #pragma map(inflate_table,"INTABL") 507 | #pragma map(inflate_fast,"INFA") 508 | #pragma map(inflate_copyright,"INCOPY") 509 | #endif 510 | 511 | #endif /* ZCONF_H */ 512 | -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/lib/zdll.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/zlib128-dll/lib/zdll.lib -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/lib/zlib.def: -------------------------------------------------------------------------------- 1 | ; zlib data compression library 2 | EXPORTS 3 | ; basic functions 4 | zlibVersion 5 | deflate 6 | deflateEnd 7 | inflate 8 | inflateEnd 9 | ; advanced functions 10 | deflateSetDictionary 11 | deflateCopy 12 | deflateReset 13 | deflateParams 14 | deflateTune 15 | deflateBound 16 | deflatePending 17 | deflatePrime 18 | deflateSetHeader 19 | inflateSetDictionary 20 | inflateGetDictionary 21 | inflateSync 22 | inflateCopy 23 | inflateReset 24 | inflateReset2 25 | inflatePrime 26 | inflateMark 27 | inflateGetHeader 28 | inflateBack 29 | inflateBackEnd 30 | zlibCompileFlags 31 | ; utility functions 32 | compress 33 | compress2 34 | compressBound 35 | uncompress 36 | gzopen 37 | gzdopen 38 | gzbuffer 39 | gzsetparams 40 | gzread 41 | gzwrite 42 | gzprintf 43 | gzvprintf 44 | gzputs 45 | gzgets 46 | gzputc 47 | gzgetc 48 | gzungetc 49 | gzflush 50 | gzseek 51 | gzrewind 52 | gztell 53 | gzoffset 54 | gzeof 55 | gzdirect 56 | gzclose 57 | gzclose_r 58 | gzclose_w 59 | gzerror 60 | gzclearerr 61 | ; large file functions 62 | gzopen64 63 | gzseek64 64 | gztell64 65 | gzoffset64 66 | adler32_combine64 67 | crc32_combine64 68 | ; checksum functions 69 | adler32 70 | crc32 71 | adler32_combine 72 | crc32_combine 73 | ; various hacks, don't look :) 74 | deflateInit_ 75 | deflateInit2_ 76 | inflateInit_ 77 | inflateInit2_ 78 | inflateBackInit_ 79 | gzgetc_ 80 | zError 81 | inflateSyncPoint 82 | get_crc_table 83 | inflateUndermine 84 | inflateResetKeep 85 | deflateResetKeep 86 | gzopen_w 87 | -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/test/example_d.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/zlib128-dll/test/example_d.exe -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/test/minigzip_d.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/zlib128-dll/test/minigzip_d.exe -------------------------------------------------------------------------------- /SDL_snake/zlib128-dll/zlib1.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_snake/zlib128-dll/zlib1.dll -------------------------------------------------------------------------------- /SDL_video_player/README.md: -------------------------------------------------------------------------------- 1 | ### 1、说明 2 | 参考ffmpeg教程示例代码,使用SDL2.0播放.mp4等格式的动画,实现了音视频同步。目前是视频同步音频,后期考虑尝试下音频同步视频(目前有一个小问题,就是在鼠标长按窗口标题栏的时候,视频虽然停住了但是音频仍在播放)。 3 | 4 | ### 2、效果演示 5 | **这里测试的是国产RPG巅峰之作,天地劫三部曲之《幽城幻剑录》的片头动画。** 6 | 7 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_video_player/screenshot/1.png) 8 | 9 | 10 | ![这里随便写文字](https://github.com/clw5180/SDL2.0-Tour-of-Game-Development/blob/master/SDL_video_player/screenshot/2.png) 11 | -------------------------------------------------------------------------------- /SDL_video_player/common.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_video_player/common.cpp -------------------------------------------------------------------------------- /SDL_video_player/common.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_video_player/common.h -------------------------------------------------------------------------------- /SDL_video_player/main.cpp: -------------------------------------------------------------------------------- 1 | // 代码参考https://github.com/samirchen/TestFFmpeg/blob/master/tutorial04/tutorial04.c 2 | // 3 | // clw note:几点注意: 4 | // 1、main函数里做的事情应该只包括ffmpeg注册,SDL初始化,创建屏幕,创建线程和event loop; 5 | // 2、其他事情比如读取数据包、进行视频解码等等都应该由其他的线程来执行; 6 | // (1)解码线程decode_thread来专门做数据包解码,这些数据包通过packet_queue_put分别被添加到 7 | // 视频和音频队列中,并分别被音频处理和视频处理部分来读取和处理。 8 | // (2)音频处理部分主要通过回调函数audio_callback,在其内部调用audio_decode_frame, 9 | // 通过packet_queue_get读取音频数据包并进行处理; 10 | // (3)视频处理部分主要通过video_thread线程通过packet_queue_get读取视频数据包并进行处理, 11 | // 显示部分主要是在调用schedule_refresh后,通过main函数接收到FF_REFRESH_EVENT事件后 12 | // 调用video_refresh_timer函数中的video_display子函数进行显示;另外video_thread在 13 | // 完成视频像素格式转换视频像素数据格式转换sws_scale之前 14 | // 会先等待main函数调用alloc_picture函数,完成SDL_CreateTexture之后再进行。 15 | // 流程图如下: 16 | /* 17 | ________ audio _______ _____ 18 | | | pkts | | | | to speaker. 19 | | DECODE | -----> | AUDIO | ---> | SDL | -- > 20 | |________| |_______| |_____| 21 | | video _______ 22 | | pkts | | 23 | +---------- > | VIDEO | 24 | _______ |_______| _______ 25 | | | | | | 26 | | EVENT | +------ > | VIDEO | to monitor. 27 | | LOOP | -------------------> | DISP. | -- > 28 | |_______| <------FF_REFRESH----|_______| 29 | */ 30 | 31 | #include "common.h" 32 | 33 | #ifdef __MINGW32__ 34 | #undef main // Prevents SDL from overriding main(). 35 | #endif 36 | 37 | //clw modify 38 | #pragma comment(lib, "legacy_stdio_definitions.lib") 39 | extern "C" { FILE __iob_func[3] = { *stdin,*stdout,*stderr }; } 40 | 41 | int quit = 0; 42 | uint8_t *audio_buff = NULL; 43 | int screen_w = 640 /*1152*/; 44 | int screen_h = 480 /*720*/; 45 | SDL_Window *screen; 46 | SDL_mutex *screen_mutex; 47 | SDL_Rect sdlRect; 48 | SDL_Renderer *sdlRenderer; 49 | /* 50 | 雷神给出的Audio Buffer结构 51 | Buffer: 52 | |-----------|-------------| 53 | chunk-------pos---len-----| 54 | static Uint8 *audio_chunk; 55 | static Uint32 audio_len; 56 | static Uint8 *audio_pos; 57 | 58 | 1帧包含1024个samples,如果每个samples是16bit(2bype)的format存储,再是2通道的, 59 | 则1帧会占用1024*2*2 = 4096个byte; 60 | 采样率如果是44100个samples每秒,那么说明每一帧的执行时间为1024samples / 44100samples/s ≈ 23.2ms 61 | 另外1s执行的帧数为44100/1024 ≈ 43.1帧 62 | */ 63 | 64 | int main(int argv, char* argc[]) 65 | { 66 | //------------SDL---------------- 67 | SDL_Event event; 68 | //-------------------------------- 69 | VideoState *is; 70 | is = (VideoState *)av_mallocz(sizeof(VideoState)); 71 | 72 | av_register_all(); //1.注册支持的文件格式及对应的codec 73 | 74 | ///////////////////////////////////////// 75 | /* clw note: SDL部分开始*/ 76 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) 77 | { 78 | fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError()); 79 | exit(1); 80 | } 81 | 82 | screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 83 | screen_w, screen_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); 84 | if (!screen) 85 | { 86 | printf("SDL: could not create window - exiting:%s\n", SDL_GetError()); 87 | return -1; 88 | } 89 | 90 | screen_mutex = SDL_CreateMutex(); 91 | 92 | sdlRenderer = SDL_CreateRenderer(screen, -1, 0); 93 | ////////////////////////////////////////////////////// 94 | is->pictq_mutex = SDL_CreateMutex(); 95 | is->pictq_cond = SDL_CreateCond(); 96 | 97 | schedule_refresh(is, 40); 98 | 99 | is->parse_tid = SDL_CreateThread(decode_thread, "decode_thread", is); 100 | if (!is->parse_tid) //clw note:tid应该是thd,线程的意思吧 101 | { 102 | av_free(is); 103 | return -1; 104 | } 105 | 106 | for (;;) 107 | { 108 | SDL_WaitEvent(&event); 109 | switch (event.type) 110 | { 111 | case SDL_QUIT: 112 | quit = 1; 113 | // If the video has finished playing, then both the picture and audio queues are waiting for more data. Make them stop waiting and terminate normally.. 114 | SDL_Quit(); 115 | return 0; 116 | case FF_REFRESH_EVENT: //clw modify 20190228:暂时先不写video_thread,而把视频部分写在主线程 117 | //TODO 118 | video_refresh_timer(event.user.data1); 119 | break; 120 | case FF_ALLOC_EVENT: 121 | alloc_picture(event.user.data1); 122 | break; 123 | case SDL_WINDOWEVENT://clw modify 20190301:SDL内部包含的事件,在操作窗口的时候触发 124 | //If Resize 125 | SDL_GetWindowSize(screen, &screen_w, &screen_h); //clw note:通过重新获得拉伸后的screen_w和screen_h然后再传入SDL_RenderCopy, 126 | //这样可以起到类似拉伸视频播放窗口的效果! 127 | break; 128 | default: 129 | break; 130 | } 131 | } 132 | 133 | return 0; 134 | } -------------------------------------------------------------------------------- /SDL_video_player/screenshot/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_video_player/screenshot/1.png -------------------------------------------------------------------------------- /SDL_video_player/screenshot/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/SDL_video_player/screenshot/2.png -------------------------------------------------------------------------------- /screenshot/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/screenshot/1.png -------------------------------------------------------------------------------- /screenshot/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/screenshot/2.png -------------------------------------------------------------------------------- /screenshot/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/screenshot/3.jpg -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/Animator/Animator.cpp: -------------------------------------------------------------------------------- 1 | #include "Animator.h" 2 | #include "AssetManager.h" 3 | 4 | Animator::Animator(sf::Sprite& sprite): m_Sprite(sprite), m_CurrentTime(), m_CurrentAnimation(nullptr) {} 5 | 6 | Animator::Animation& Animator::CreateAnimation(std::string const& name, 7 | std::string const& textureName, 8 | sf::Time const& duration, bool loop) 9 | { 10 | m_Animations.push_back(Animator::Animation(name, textureName, duration, loop)); 11 | 12 | // If we don't have any other animations, use that as current animation 13 | if (m_CurrentAnimation == nullptr) SwitchAnimation(&m_Animations.back()); 14 | 15 | return m_Animations.back(); 16 | } 17 | 18 | Animator::Animation* Animator::FindAnimation(std::string const& name) 19 | { 20 | for (auto it = m_Animations.begin(); it != m_Animations.end(); ++it) 21 | { 22 | if (it->m_Name == name) return &*it; 23 | } 24 | 25 | return nullptr; 26 | } 27 | 28 | void Animator::Update(sf::Time const& dt) 29 | { 30 | // If we don't have any animations yet return 31 | if (m_CurrentAnimation == nullptr) return; 32 | 33 | m_CurrentTime += dt; 34 | 35 | // Get the current animation frame 36 | float scaledTime = 37 | (m_CurrentTime.asSeconds() / m_CurrentAnimation->m_Duration.asSeconds()); 38 | int numFrames = m_CurrentAnimation->m_Frames.size(); 39 | int currentFrame = static_cast(scaledTime * numFrames); 40 | 41 | // If the animation is looping, calculate the correct frame 42 | if (m_CurrentAnimation->m_Looping) 43 | currentFrame %= numFrames; 44 | else if (currentFrame >= 45 | numFrames) // if the current frame is greater than the number of frames 46 | currentFrame = numFrames - 1; // Show last frame 47 | 48 | // Set the texture rectangle, depending on the frame 49 | m_Sprite.setTextureRect(m_CurrentAnimation->m_Frames[currentFrame]); 50 | } 51 | 52 | bool Animator::SwitchAnimation(std::string const& name) 53 | { 54 | auto animation = FindAnimation(name); 55 | if (animation != nullptr) 56 | { 57 | SwitchAnimation(animation); 58 | return true; 59 | } 60 | 61 | return false; 62 | } 63 | 64 | void Animator::SwitchAnimation(Animator::Animation* animation) 65 | { 66 | // Change the sprite texture 67 | if (animation != nullptr) 68 | { 69 | m_Sprite.setTexture(AssetManager::GetTexture(animation->m_TextureName)); 70 | } 71 | 72 | m_CurrentAnimation = animation; 73 | m_CurrentTime = sf::Time::Zero; // Reset the time 74 | } 75 | 76 | std::string Animator::GetCurrentAnimationName() const 77 | { 78 | if (m_CurrentAnimation != nullptr) return m_CurrentAnimation->m_Name; 79 | 80 | // If no animation is playing, return empty string 81 | return ""; 82 | } 83 | -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/Animator/Animator.h: -------------------------------------------------------------------------------- 1 | #ifndef ANIMATOR_H 2 | #define ANIMATOR_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Animator { 9 | public: 10 | struct Animation { 11 | std::string m_Name; 12 | std::string m_TextureName; 13 | std::vector m_Frames; 14 | sf::Time m_Duration; 15 | bool m_Looping; 16 | 17 | Animation(std::string const& name, std::string const& textureName, 18 | sf::Time const& duration, bool looping) 19 | : m_Name(name), 20 | m_TextureName(textureName), 21 | m_Duration(duration), 22 | m_Looping(looping) {} 23 | 24 | // Adds frames horizontally 25 | void AddFrames(sf::Vector2i const& startFrom, sf::Vector2i const& frameSize, 26 | unsigned int frames) { 27 | sf::Vector2i current = startFrom; 28 | for (unsigned int i = 0; i < frames; i++) { 29 | // Add current frame from position and frame size 30 | m_Frames.push_back(sf::IntRect(current.x, current.y, frameSize.x, frameSize.y)); 31 | // Advance current frame horizontally 32 | current.x += frameSize.x; 33 | } 34 | } 35 | }; 36 | 37 | Animator(sf::Sprite& sprite); 38 | 39 | Animator::Animation& CreateAnimation(std::string const& name, 40 | std::string const& textureName, 41 | sf::Time const& duration, bool loop = false); 42 | 43 | void Update(sf::Time const& dt); 44 | 45 | // Returns if the switch was successful 46 | bool SwitchAnimation(std::string const& name); 47 | 48 | std::string GetCurrentAnimationName() const; 49 | 50 | private: 51 | // Returns the animation with the passed name 52 | // Returns nullptr if no such animation is found 53 | Animator::Animation* FindAnimation(std::string const& name); 54 | 55 | void SwitchAnimation(Animator::Animation* animation); 56 | 57 | // Reference to the sprite 58 | sf::Sprite& m_Sprite; 59 | sf::Time m_CurrentTime; 60 | std::list m_Animations; 61 | Animator::Animation* m_CurrentAnimation; 62 | }; 63 | 64 | #endif -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/Animator/AssetManager.cpp: -------------------------------------------------------------------------------- 1 | #include "AssetManager.h" 2 | #include 3 | 4 | AssetManager* AssetManager::sInstance = nullptr; 5 | 6 | AssetManager::AssetManager() 7 | { 8 | // Only allow one AssetManager to exist 9 | // Otherwise throw an exception 10 | assert(sInstance == nullptr); 11 | sInstance = this; 12 | } 13 | 14 | sf::Texture& AssetManager::GetTexture(std::string const& filename) 15 | { 16 | auto& texMap = sInstance->m_Textures; 17 | 18 | // See if the texture is already loaded 19 | auto pairFound = texMap.find(filename); 20 | // If yes, return the texture 21 | if (pairFound != texMap.end()) 22 | { 23 | return pairFound->second; 24 | } 25 | else // Else, load the texture and return it 26 | { 27 | // Create an element in the texture map 28 | auto& texture = texMap[filename]; 29 | texture.loadFromFile(filename); 30 | return texture; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/Animator/AssetManager.h: -------------------------------------------------------------------------------- 1 | #ifndef ASSET_MANAGER_H 2 | #define ASSET_MANAGER_H 3 | 4 | #include 5 | #include 6 | 7 | class AssetManager 8 | { 9 | public: 10 | AssetManager(); 11 | 12 | static sf::Texture& GetTexture(std::string const& filename); 13 | 14 | private: 15 | std::map m_Textures; 16 | 17 | // AssetManager is a singleton, so only one instance can exist at a time 18 | // sInstacne holds a static pointer to the single manager instance 19 | static AssetManager* sInstance; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/Animator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Animator.h" 4 | #include "AssetManager.h" 5 | 6 | int main() { 7 | sf::RenderWindow window(sf::VideoMode(640, 480), "AssetManager"); 8 | AssetManager manager; 9 | 10 | sf::Vector2i spriteSize(32, 32); 11 | sf::Sprite sprite; 12 | Animator animator(sprite); 13 | // Idle animation with 8 frames @ 1 sec looping 14 | auto& idleAnimation = 15 | animator.CreateAnimation("Idle", "spriteSheet.png", sf::seconds(1), true); 16 | idleAnimation.AddFrames(sf::Vector2i(0, 0), spriteSize, 8); 17 | 18 | // IdleShort animation with 8 frames @ 0.5 sec looping 19 | auto& idleAnimationShort = 20 | animator.CreateAnimation("IdleShort", "spriteSheet.png", sf::seconds(0.5f), true); 21 | idleAnimationShort.AddFrames(sf::Vector2i(0, 0), spriteSize, 8); 22 | 23 | // IdleSmall animation with 5 frames @ 1.5 sec looping 24 | auto& idleAnimationSmall = 25 | animator.CreateAnimation("IdleSmall", "myTexture.png", sf::seconds(1.5f), true); 26 | // Adding frames multiple times from different locations 27 | idleAnimationSmall.AddFrames(sf::Vector2i(64, 0), spriteSize, 3); 28 | idleAnimationSmall.AddFrames(sf::Vector2i(64, 32), spriteSize, 2); 29 | 30 | // IdleOnce animation with 8 frames @ 0.5 sec not looping 31 | auto& idleAnimationOnce = 32 | animator.CreateAnimation("IdleOnce", "myTexture.png", sf::seconds(0.5f), false); 33 | idleAnimationOnce.AddFrames(sf::Vector2i(0, 0), spriteSize, 8); 34 | 35 | sf::Clock clock; 36 | while (window.isOpen()) 37 | { 38 | // Returns the elapsed time and restarts the clock 39 | sf::Time deltaTime = clock.restart(); 40 | 41 | sf::Event ev; 42 | while (window.pollEvent(ev)) 43 | { 44 | if (ev.type == sf::Event::KeyPressed) 45 | { 46 | if (ev.key.code == sf::Keyboard::Key::Num1) 47 | animator.SwitchAnimation("Idle"); 48 | else if (ev.key.code == sf::Keyboard::Key::Num2) 49 | animator.SwitchAnimation("IdleShort"); 50 | else if (ev.key.code == sf::Keyboard::Key::Num3) 51 | animator.SwitchAnimation("IdleSmall"); 52 | else if (ev.key.code == sf::Keyboard::Key::Num4) 53 | animator.SwitchAnimation("IdleOnce"); 54 | } 55 | } 56 | 57 | animator.Update(deltaTime); 58 | 59 | window.clear(sf::Color::Black); 60 | 61 | window.draw(sprite); 62 | 63 | window.display(); 64 | } 65 | 66 | // After main() returns, the manager is destroyed 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/Animator/myTexture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Animating Sprites/Animator/myTexture.png -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/Animator/spritesheet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Animating Sprites/Animator/spritesheet.png -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/test animation/AssetManager.cpp: -------------------------------------------------------------------------------- 1 | #include "AssetManager.h" 2 | #include 3 | 4 | AssetManager* AssetManager::sInstance = nullptr; 5 | 6 | AssetManager::AssetManager() 7 | { 8 | // Only allow one AssetManager to exist 9 | // Otherwise throw an exception 10 | assert(sInstance == nullptr); 11 | sInstance = this; 12 | } 13 | 14 | sf::Texture& AssetManager::GetTexture(std::string const& filename) 15 | { 16 | auto& texMap = sInstance->m_Textures; 17 | 18 | // See if the texture is already loaded 19 | auto pairFound = texMap.find(filename); 20 | // If yes, return the texture 21 | if (pairFound != texMap.end()) 22 | { 23 | return pairFound->second; 24 | } 25 | else // Else, load the texture and return it 26 | { 27 | // Create an element in the texture map 28 | auto& texture = texMap[filename]; 29 | texture.loadFromFile(filename); 30 | return texture; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/test animation/AssetManager.h: -------------------------------------------------------------------------------- 1 | #ifndef ASSET_MANAGER_H 2 | #define ASSET_MANAGER_H 3 | 4 | #include 5 | #include 6 | 7 | class AssetManager 8 | { 9 | public: 10 | AssetManager(); 11 | 12 | static sf::Texture& GetTexture(std::string const& filename); 13 | 14 | private: 15 | std::map m_Textures; 16 | 17 | // AssetManager is a singleton, so only one instance can exist at a time 18 | // sInstacne holds a static pointer to the single manager instance 19 | static AssetManager* sInstance; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /《SFML Essentials》/Animating Sprites/test animation/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "AssetManager.h" 3 | 4 | int main() 5 | { 6 | sf::RenderWindow window(sf::VideoMode(640, 480), "cyrstal animation"); 7 | AssetManager manager; 8 | 9 | sf::Vector2i spriteSize(32, 32); 10 | sf::Sprite sprite(AssetManager::GetTexture("crystal.png")); 11 | // Set the sprite image to the first frame of the animation 12 | sprite.setTextureRect(sf::IntRect(0, 0, spriteSize.x, spriteSize.y)); 13 | 14 | int frameNum = 8; //Animation consists of 8 frames 15 | float animationDuration = 1; // 1 seconds 16 | 17 | sf::Time deltaTime; 18 | sf::Time elapsedTime; 19 | sf::Clock clock; 20 | 21 | while (window.isOpen()) 22 | { 23 | // Returns the elapsed time and restarts the clock 24 | deltaTime = clock.restart(); 25 | 26 | //Handle input 27 | sf::Event event; 28 | while (window.pollEvent(event)) 29 | { 30 | switch (event.type) 31 | { 32 | case sf::Event::EventType::Closed: 33 | window.close(); 34 | break; 35 | case sf::Event::EventType::KeyPressed: 36 | if (event.key.code == sf::Keyboard::Escape) window.close(); 37 | break; 38 | default: 39 | break; 40 | } 41 | } 42 | //Accumulate time with each time 43 | elapsedTime += deltaTime; 44 | float timeAsSeconds = elapsedTime.asSeconds(); 45 | 46 | //Get the current animation frame 47 | int animFrame = static_cast((timeAsSeconds / animationDuration)*frameNum) % frameNum; 48 | 49 | // Set the texture rectangle, depending on the frame 50 | sprite.setTextureRect(sf::IntRect(animFrame *spriteSize.x, 0, spriteSize.x, spriteSize.y)); 51 | 52 | // Render frame 53 | window.clear(sf::Color::Black); 54 | window.draw(sprite); 55 | window.display(); 56 | } 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /《SFML Essentials》/Assets/crystal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Assets/crystal.png -------------------------------------------------------------------------------- /《SFML Essentials》/Assets/cube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Assets/cube.png -------------------------------------------------------------------------------- /《SFML Essentials》/Assets/cube_tiled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Assets/cube_tiled.png -------------------------------------------------------------------------------- /《SFML Essentials》/Assets/egg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Assets/egg.png -------------------------------------------------------------------------------- /《SFML Essentials》/Assets/leaf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Assets/leaf.png -------------------------------------------------------------------------------- /《SFML Essentials》/Helloworld/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Helloworld/main.cpp -------------------------------------------------------------------------------- /《SFML Essentials》/Loading and Using Textures/AssetManagement/AssetManager.cpp: -------------------------------------------------------------------------------- 1 | #include "AssetManager.h" 2 | #include 3 | 4 | AssetManager* AssetManager::sInstance = nullptr; 5 | 6 | AssetManager::AssetManager() 7 | { 8 | // Only allow one AssetManager to exist 9 | // Otherwise throw an exception 10 | assert(sInstance == nullptr); 11 | sInstance = this; 12 | } 13 | 14 | sf::Texture& AssetManager::GetTexture(std::string const& filename) 15 | { 16 | auto& texMap = sInstance->m_Textures; 17 | 18 | // See if the texture is already loaded 19 | auto pairFound = texMap.find(filename); 20 | // If yes, return the texture 21 | if (pairFound != texMap.end()) 22 | { 23 | return pairFound->second; 24 | } 25 | else // Else, load the texture and return it 26 | { 27 | // Create an element in the texture map 28 | auto& texture = texMap[filename]; 29 | texture.loadFromFile(filename); 30 | return texture; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /《SFML Essentials》/Loading and Using Textures/AssetManagement/AssetManager.h: -------------------------------------------------------------------------------- 1 | #ifndef ASSET_MANAGER_H 2 | #define ASSET_MANAGER_H 3 | 4 | #include 5 | #include 6 | 7 | class AssetManager 8 | { 9 | public: 10 | AssetManager(); 11 | 12 | static sf::Texture& GetTexture(std::string const& filename); 13 | 14 | private: 15 | std::map m_Textures; 16 | 17 | // AssetManager is a singleton, so only one instance can exist at a time 18 | // sInstacne holds a static pointer to the single manager instance 19 | static AssetManager* sInstance; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /《SFML Essentials》/Loading and Using Textures/AssetManagement/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "AssetManager.h" 3 | 4 | int main() 5 | { 6 | sf::RenderWindow window(sf::VideoMode(640, 480), "AssetManager"); 7 | AssetManager manager; 8 | 9 | // Create sprites 10 | sf::Sprite sprite1 = sf::Sprite(AssetManager::GetTexture("myTexture1.png")); 11 | sf::Sprite sprite2 = sf::Sprite(AssetManager::GetTexture("myTexture2.png")); 12 | sf::Sprite sprite3 = sf::Sprite(AssetManager::GetTexture("myTexture3.png")); 13 | 14 | while (window.isOpen()) 15 | { 16 | // Game loop 17 | } 18 | 19 | // After main() returns, the manager is destroyed 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /《SFML Essentials》/Loading and Using Textures/AssetManagement/myTexture1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Loading and Using Textures/AssetManagement/myTexture1.png -------------------------------------------------------------------------------- /《SFML Essentials》/Loading and Using Textures/AssetManagement/myTexture2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Loading and Using Textures/AssetManagement/myTexture2.png -------------------------------------------------------------------------------- /《SFML Essentials》/Loading and Using Textures/cube.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Vertex { 4 | sf::Vector2f Position; 5 | sf::Vector2f TexCoord; 6 | }; 7 | 8 | int main01() { 9 | sf::RenderWindow window(sf::VideoMode(640, 480), "Textures"); 10 | // Set target Frames per second 11 | window.setFramerateLimit(60); 12 | 13 | sf::Texture texture; 14 | texture.loadFromFile("cube.png"); 15 | // Set the texture in repeat mode 16 | //texture.setRepeated(true); 17 | 18 | sf::RectangleShape rectShape(sf::Vector2f(128*3, 221*3)); 19 | // Bigger texture rectangle than the size of the texture 20 | rectShape.setTextureRect(sf::IntRect(0, 0, 128*3, 221*3)); 21 | rectShape.setTexture(&texture); 22 | 23 | while (window.isOpen()) { 24 | // Handle events 25 | sf::Event event; 26 | while (window.pollEvent(event)) { 27 | switch (event.type) { 28 | case sf::Event::EventType::Closed: 29 | window.close(); 30 | break; 31 | case sf::Event::EventType::KeyPressed: 32 | if (event.key.code == sf::Keyboard::Escape) window.close(); 33 | break; 34 | default: 35 | break; 36 | } 37 | } 38 | 39 | window.clear(sf::Color::Black); 40 | window.draw(rectShape); 41 | window.display(); 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /《SFML Essentials》/Loading and Using Textures/cube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Essentials》/Loading and Using Textures/cube.png -------------------------------------------------------------------------------- /《SFML Essentials》/Simple Game/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // 如果我们用Window类,我们必须使用头文件#include 3 | 4 | void initShape(sf::RectangleShape &shape, sf::Vector2f const &pos, sf::Color const &color) 5 | { 6 | shape.setFillColor(color); 7 | shape.setPosition(pos); 8 | shape.setOrigin(shape.getSize() * 0.5f); // The center of the rectangle 9 | } 10 | 11 | int main() 12 | { 13 | sf::RenderWindow window(sf::VideoMode(482, 180), "Bad Squares!"); //创建窗口代码 14 | // sf::sleep(sf::seconds(3)); //这样我们就可以创建窗口之后,运行代码可以看到窗口,否则不可以 15 | 16 | window.setFramerateLimit(60); //设置每秒目标帧数 17 | 18 | sf::Vector2f startPos = sf::Vector2f(50, 50); 19 | sf::RectangleShape playerRect(sf::Vector2f(50, 50)); 20 | initShape(playerRect, startPos, sf::Color::Green); 21 | 22 | sf::RectangleShape targetRect(sf::Vector2f(50, 50)); 23 | initShape(targetRect, sf::Vector2f(400, 50), sf::Color::Blue); 24 | 25 | sf::RectangleShape badRect(sf::Vector2f(50, 100)); 26 | initShape(badRect, sf::Vector2f(250, 50), sf::Color::Red); 27 | 28 | while (window.isOpen()) 29 | { 30 | //Handle events 31 | sf::Event event; 32 | while (window.pollEvent(event)) 33 | { 34 | if (event.type == sf::Event::Closed) 35 | window.close(); 36 | } 37 | 38 | //Update frame 39 | playerRect.move(1, 0); //总是向右移动 40 | if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) 41 | { 42 | playerRect.move(0, 1); 43 | } 44 | if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) 45 | { 46 | playerRect.move(0, -1); 47 | } 48 | //达到了目标。 你赢了。 退出游戏 49 | if (playerRect.getGlobalBounds().intersects(targetRect.getGlobalBounds())) 50 | { 51 | window.close(); 52 | } 53 | // 你碰到了敌人,输了,重新开始! 54 | if (playerRect.getGlobalBounds().intersects(badRect.getGlobalBounds())) 55 | { 56 | playerRect.setPosition(startPos); 57 | } 58 | //Render frame 59 | window.clear(); 60 | window.draw(playerRect); 61 | window.draw(targetRect); 62 | window.draw(badRect); 63 | window.display(); 64 | } 65 | system("pause"); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter1/game.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Game Development》/Chapter1/game.cpp -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter1/game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | class Game : private sf::NonCopyable //TODO 6 | { 7 | public: 8 | Game(); 9 | void run(); 10 | 11 | private: 12 | void processEvents(); 13 | void handlePlayerInput(sf::Keyboard::Key key, bool isPressed); 14 | void update(sf::Time deltaTime); 15 | void render(); 16 | 17 | static const float playerSpeed; 18 | static const sf::Time timePerFrame; 19 | 20 | sf::RenderWindow mWindow; 21 | //sf::CircleShape mPlayer; 22 | sf::Texture mTexture; 23 | sf::Sprite mPlayer; 24 | 25 | bool mIsMovingUp; 26 | bool mIsMovingDown; 27 | bool mIsMovingLeft; 28 | bool mIsMovingRight; 29 | }; 30 | 31 | -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter1/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "game.h" 3 | 4 | int main() 5 | { 6 | Game game; 7 | game.run(); 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter1/res/Eagle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Game Development》/Chapter1/res/Eagle.png -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter2/ResourceHolder.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _RESOURCEHOLDER_HPP_ 2 | #define _RESOURCEHOLDER_HPP_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | template 12 | class ResourceHolder 13 | { 14 | public: 15 | void load(Identifier id, const std::string& filename); 16 | 17 | template 18 | void load(Identifier id, const std::string& filename, const Parameter& secondParam); 19 | 20 | Resource& get(Identifier id); 21 | const Resource& get(Identifier id) const; 22 | 23 | 24 | private: 25 | void insertResource(Identifier id, std::unique_ptr resource); 26 | 27 | 28 | private: 29 | std::map> mResourceMap; 30 | }; 31 | 32 | #include "ResourceHolder.inl" 33 | #endif // BOOK_RESOURCEHOLDER_HPP 34 | -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter2/ResourceHolder.inl: -------------------------------------------------------------------------------- 1 | 2 | template 3 | void ResourceHolder::load(Identifier id, const std::string& filename) 4 | { 5 | // Create and load resource 6 | std::unique_ptr resource(new Resource()); 7 | if (!resource->loadFromFile(filename)) 8 | throw std::runtime_error("ResourceHolder::load - Failed to load " + filename); 9 | 10 | // If loading successful, insert resource to map 11 | insertResource(id, std::move(resource)); 12 | } 13 | 14 | template 15 | template 16 | void ResourceHolder::load(Identifier id, const std::string& filename, const Parameter& secondParam) 17 | { 18 | // Create and load resource 19 | std::unique_ptr resource(new Resource()); 20 | if (!resource->loadFromFile(filename, secondParam)) 21 | throw std::runtime_error("ResourceHolder::load - Failed to load " + filename); 22 | 23 | // If loading successful, insert resource to map 24 | insertResource(id, std::move(resource)); 25 | } 26 | 27 | template 28 | Resource& ResourceHolder::get(Identifier id) 29 | { 30 | auto found = mResourceMap.find(id); 31 | assert(found != mResourceMap.end()); 32 | 33 | return *found->second; 34 | } 35 | 36 | template 37 | const Resource& ResourceHolder::get(Identifier id) const 38 | { 39 | auto found = mResourceMap.find(id); 40 | assert(found != mResourceMap.end()); 41 | 42 | return *found->second; 43 | } 44 | 45 | template 46 | void ResourceHolder::insertResource(Identifier id, std::unique_ptr resource) 47 | { 48 | // Insert and check success 49 | auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource))); 50 | assert(inserted.second); 51 | } 52 | -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter2/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "resourceholder.hpp" 4 | 5 | // Resource ID for sf::Texture 6 | namespace Textures 7 | { 8 | enum ID 9 | { 10 | Landscape, 11 | Airplane, 12 | }; 13 | } 14 | 15 | int main() 16 | { 17 | sf::RenderWindow window(sf::VideoMode(640, 480), "Resources"); 18 | window.setFramerateLimit(20); 19 | 20 | // Try to load resources 21 | ResourceHolder textures; 22 | try 23 | { 24 | textures.load(Textures::Landscape, "./res/Desert.png"); 25 | textures.load(Textures::Airplane, "./res/Eagle.png"); 26 | } 27 | catch (std::runtime_error& e) 28 | { 29 | std::cout << "Exception: " << e.what() << std::endl; 30 | return 1; 31 | } 32 | 33 | // Access resources 34 | sf::Sprite landscape(textures.get(Textures::Landscape)); 35 | sf::Sprite airplane(textures.get(Textures::Airplane)); 36 | airplane.setPosition(200.f, 200.f); 37 | 38 | while (window.isOpen()) 39 | { 40 | sf::Event event; 41 | while (window.pollEvent(event)) 42 | { 43 | if (event.type == sf::Event::KeyPressed || event.type == sf::Event::Closed) 44 | return 0; 45 | } 46 | 47 | window.clear(); 48 | window.draw(landscape); 49 | window.draw(airplane); 50 | window.display(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter2/res/Desert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Game Development》/Chapter2/res/Desert.png -------------------------------------------------------------------------------- /《SFML Game Development》/Chapter2/res/Eagle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clw5180/SDL_and_SFML_RPG_Game/5b19ebc655e11474cf334be777f119867733564d/《SFML Game Development》/Chapter2/res/Eagle.png --------------------------------------------------------------------------------