00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "draw.h"
00019 #include "../Util/StringUtil.h"
00020 #include "../Util/STL_Helper.h"
00021 #include <vector>
00022 #include <iostream>
00023 #include <cmath>
00024
00025 using namespace std;
00026 using namespace Graphics;
00027
00028 const int SCREEN_WIDTH=640;
00029 const int SCREEN_HEIGHT=480;
00030 const int NUM_IMAGES=10;
00031
00032 enum Direction {
00033 RIGHT,
00034 LEFT,
00035 DOWN,
00036 UP
00037 };
00038
00039 int main(int argc, char* argv[]) {
00040 std::vector<SDL_Surface*> surfVec;
00041 SDL_Surface* srcSurf=NULL;
00042 try {
00043 std::string filename;
00044 std::string extensionlessName;
00045 std::string extension;
00046 Direction direction;
00047 int delta;
00048 int numFrames;
00049
00050 cout << "Enter filename ";
00051 cin >> filename;
00052
00053 srcSurf = SDL_LoadBMP(filename.c_str());
00054 if (srcSurf == NULL) {
00055 cout << "Error, couldn't read file " << filename << endl;
00056 return 1;
00057 }
00058
00059 extension = filename.substr(filename.rfind('.'));
00060 extensionlessName = filename.substr(0, filename.length()- extension.length());
00061
00062
00063
00064 delta=1;
00065
00066 cout << "Enter number of frames ";
00067
00068 numFrames=10;
00069
00070 cout << "Enter direction" << endl;
00071 cout << "1. Right" << endl;
00072 cout << "2. Left" << endl;
00073 cout << "3. Up" << endl;
00074 cout << "4. Down" << endl;
00075
00076 int dirChoice;
00077 cin >> dirChoice;
00078
00079 switch (dirChoice) {
00080 case 1: direction = RIGHT; break;
00081 case 2: direction = LEFT; break;
00082 case 3: direction = UP; break;
00083 case 4: direction = DOWN; break;
00084 default:
00085 cout << "invalid direction" << endl;
00086 return 0;
00087 }
00088
00089 surfVec.resize(numFrames);
00090
00091 int w = srcSurf->w;
00092 int h = srcSurf->h;
00093
00094 cout << "Writing animation file at " << extensionlessName << ".ani" << endl;
00095 ofstream animationFile((extensionlessName + ".ani").c_str());
00096 animationFile << numFrames << endl;
00097 animationFile << 1 << endl;
00098
00099 for (int i = 0; i < numFrames; i++) {
00100
00101 surfVec[i] = SDL_LoadBMP(filename.c_str());
00102 SDL_LockSurface(surfVec[i]);
00103 int x,y;
00104 int destLoc;
00105 switch (direction) {
00106 case LEFT:
00107 case RIGHT:
00108 for (x = 0; x < w; x++) {
00109 destLoc = (x + i * delta) % w;
00110 for (y = 0; y <h; y++) {
00111 if (direction == RIGHT) {
00112 putpixel(surfVec[i],destLoc, y, getpixel(srcSurf, x, y));
00113 } else {
00114 putpixel(surfVec[i], x, y, getpixel(srcSurf, destLoc,y));
00115 }
00116 }
00117 }
00118 break;
00119 case UP:
00120 case DOWN:
00121 for (y = 0; y < h; y++) {
00122 destLoc = (y + i * delta) %h;
00123 for (x = 0; x < w; x++) {
00124 if (direction == DOWN) {
00125 putpixel(surfVec[i], x , destLoc, getpixel(srcSurf, x , y));
00126 } else {
00127 putpixel(surfVec[i], x, y, getpixel(srcSurf, x, destLoc));
00128 }
00129 }
00130 }
00131 break;
00132 default: break;
00133 }
00134 std::string newFilename = extensionlessName + Util::toString(i) + extension;
00135 cout << "Writing to " << newFilename << endl;
00136 SDL_SaveBMP(surfVec[i], newFilename.c_str());
00137 animationFile << newFilename << endl;
00138 }
00139 animationFile.close();
00140
00141 SDL_Init(SDL_INIT_VIDEO);
00142 SDL_Surface* screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16,
00143 SDL_SWSURFACE| SDL_DOUBLEBUF);
00144
00145 std::vector<AnimatedImage*> images;
00146 images.resize(NUM_IMAGES);
00147 for (int i = 0; i < NUM_IMAGES; i++) {
00148 images[i] = new AnimatedImage(extensionlessName + ".ani");
00149 }
00150
00151 while (!SDL_GetKeyState(NULL)[SDLK_ESCAPE]) {
00152 SDL_Rect r;
00153 r.y=r.x=100;
00154 SDL_PumpEvents();
00155 for (int x = 0; x < NUM_IMAGES/2; x++) {
00156 r.y=0;
00157
00158 images[x]->drawAt(screen, &r);
00159
00160
00161 r.x+=w;
00162 }
00163
00164 for (int y = NUM_IMAGES/2; y < NUM_IMAGES; y++) {
00165 images[y]->drawAt(screen, &r);
00166 r.y+=h;
00167 }
00168
00169 SDL_Delay(50);
00170 SDL_Flip(screen);
00171 }
00172 Util::freeDynamicContainer(images);
00173 } catch (Util::GeneralException& e) {
00174 cout << "Fatal exception:" << e << endl;
00175 }
00176 SDL_UnlockSurface(srcSurf);
00177 SDL_FreeSurface(srcSurf);
00178 for (std::vector<SDL_Surface*>::iterator it = surfVec.begin(); it != surfVec.end(); it++) {
00179 SDL_UnlockSurface(*it);
00180 SDL_FreeSurface(*it);
00181 }
00182 Graphics::ImageManager::destroy();
00183 SDL_Quit();
00184 return 0;
00185 }