OpenCores
URL https://opencores.org/ocsvn/orsoc_graphics_accelerator/orsoc_graphics_accelerator/trunk

Subversion Repositories orsoc_graphics_accelerator

[/] [orsoc_graphics_accelerator/] [trunk/] [sw/] [utils/] [spritemaker.cpp] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 maiden
/*
2
Per Lenander 2008
3
 
4
Aniconv tool:
5
-------------
6
 
7
The tool utilize SDL/SDL_image for graphics conversion
8
*/
9
 
10
#include <SDL/SDL.h>
11
#include <SDL/SDL_image.h>
12
 
13
#define SCREEN_WIDTH 640
14
#define SCREEN_HEIGHT 480
15
#define SCREEN_DEPTH 32
16
 
17
#include <sstream>
18
#include <iostream>
19
#include <fstream>
20
#include <cstdlib>
21
 
22
using namespace std;
23
 
24
SDL_Surface *screen;
25
 
26
int spriteW = 0;
27
int spriteH = 0;
28
 
29
/* Ordinary inits */
30
int InitSDL(void)
31
{
32
        if (SDL_Init(SDL_INIT_VIDEO) < 0)
33
        {
34
                cerr << "Unable to init SDL: " << SDL_GetError() << endl;
35
                return 1;
36
        }
37
 
38
        screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF);
39
 
40
        if (!screen)
41
        {
42
                cerr << "Unable to set video: " << SDL_GetError() << endl;
43
                return 1;
44
        }
45
        // make sure SDL cleans up before exit
46
        atexit(SDL_Quit);
47
 
48
        return 0;
49
};
50
 
51
/* Loads the spritesheet (or any image, but this is the only thing it is used for) */
52
SDL_Surface * LoadImage(std::string s)
53
{
54
        SDL_Surface *loaded = NULL;
55
 
56
        loaded = IMG_Load(s.c_str());
57
 
58
        if(!loaded)             return NULL;
59
 
60
        SDL_Surface *optimized = NULL;
61
 
62
        optimized = SDL_DisplayFormatAlpha(loaded);
63
 
64
        SDL_FreeSurface(loaded);
65
 
66
        if(!optimized)          return NULL;
67
 
68
        return optimized;
69
};
70
 
71
SDL_Color GetPixel ( SDL_Surface* pSurface , int x , int y )
72
{
73
  SDL_Color color ;
74
  Uint32 col = 0 ;
75
 
76
  //determine position
77
  char* pPosition = ( char* ) pSurface->pixels ;
78
 
79
  //offset by y
80
  pPosition += ( pSurface->pitch * y ) ;
81
 
82
  //offset by x
83
  pPosition += ( pSurface->format->BytesPerPixel * x ) ;
84
 
85
  //copy pixel data
86
  memcpy ( &col , pPosition , pSurface->format->BytesPerPixel ) ;
87
 
88
  //convert color
89
  SDL_GetRGB ( col , pSurface->format , &color.r , &color.g , &color.b ) ;
90
  return ( color ) ;
91
}
92
 
93
int main ( int argc, char** argv )
94
{
95
        if( argc < 2 )
96
        {
97
        cout << "Usage: " << argv[0] << " filename.<jpg/png/bmp> [bpp=16]" << endl;
98
                return 1;
99
        }
100
 
101
        if( InitSDL() )
102
                return 1;
103
        cout << "SDL successfully initialized." << endl;
104
 
105
        SDL_Surface *spritesheet = LoadImage( argv[1] );
106
        if( spritesheet == NULL )
107
        {
108
                cout << "Couldn't load " << argv[1] << "!" << endl;
109
                return 1;
110
        }
111
 
112
        /* Open an output file */
113
        string          filename(argv[1]);
114
        filename        += ".h";
115
        ofstream        output(filename.c_str(),ofstream::out);
116
        if( !output )
117
        {
118
                cout << "Couldn't open output file!" << endl;
119
                return 1;
120
        }
121
 
122
    int outputBpp = 16;
123
        if(argc >= 3)
124
        {
125
                stringstream ss(argv[2]);
126
                ss >> outputBpp;
127
 
128
                if(outputBpp != 8 && outputBpp != 16 && outputBpp != 24 && outputBpp != 32)
129
                {
130
                        cout << "Mode: '" << argv[2] << "' not supported, choose between 8, 16, 24, and 32" << endl;
131
                        return 1;
132
                }
133
                else if(outputBpp == 8 && spritesheet->w % 4)
134
                {
135
                        cout << "Mode: 8bpp requires the width to be divisible by 4!" << endl;
136
                        return 1;
137
                }
138
                else if(outputBpp == 16 && spritesheet->w % 2)
139
                {
140
                        cout << "Mode: 16bpp requires the width to be divisible by 2!" << endl;
141
                        return 1;
142
                }
143
        }
144
 
145
        int stride = 1;
146
        if(outputBpp == 8)
147
                stride = 4;
148
        else if(outputBpp == 16)
149
                stride = 2;
150
 
151
        filename = filename.substr(0, filename.find('.'));
152
 
153
        output << "/* Sprite definition */" << endl << endl;
154
        output << "/* size: " << spritesheet->w << " * " << spritesheet->h << " at " << outputBpp << "BPP */" << endl << endl;
155
        output << "#ifndef " << filename << "_H" << endl;
156
    output << "#define " << filename << "_H" << endl << endl;
157
 
158
    output << "#include \"orgfx_plus.h\"" << endl << endl;
159
 
160
 
161
    output << "// This sprite can be loaded by calling:" << endl;
162
    output << "int init_" << filename << "_sprite();" << endl << endl;
163
 
164
    output << "// The return value is a loaded surface using the orgfxplus_init_surface method" << endl << endl;
165
 
166
        output << "unsigned int " << filename << "[] = {" << endl;
167
 
168
        for(int y = 0; y < spritesheet->h; y++)
169
        {
170
                for(int x = 0; x < spritesheet->w; x+=stride)
171
                {
172
                        unsigned int pixel = 0;
173
                        for(int s = 0; s < stride; s++)
174
                        {
175
                                SDL_Color c = GetPixel(spritesheet, x+s, y);
176
 
177
                                if(outputBpp == 8)
178
                                {
179
                                        pixel += (unsigned char)(0.3 * c.r + 0.59 * c.g + 0.11 * c.b);
180
                                        if(s != 3) pixel = pixel << 8;
181
                                }
182
                                else if(outputBpp == 16)
183
                                {
184
                                        c.r = c.r >> 3;
185
                                        c.g = c.g >> 2;
186
                                        c.b = c.b >> 3;
187
                                        pixel += (c.r << 11) | (c.g << 5) | c.b;
188
                                        if(s != 1) pixel = pixel << 16;
189
                                }
190
                                else
191
                                        pixel = (c.r << 16) | (c.g << 8) | c.b;
192
                        }
193
                        output << pixel << "u, ";
194
                }
195
                output << endl;
196
        }
197
 
198
        output << "};" << endl << endl;
199
 
200
    output << "int init_" << filename << "_sprite()" << endl;
201
    output << "{" << endl;
202
    output << "  return orgfxplus_init_surface(" << spritesheet->w << ", " << spritesheet->h << ", " << filename << ");" << endl;
203
    output << "}" << endl << endl;
204
 
205
        output << "#endif // " << filename << "_H" << endl;
206
 
207
        output.close();
208
        SDL_FreeSurface( spritesheet );
209
 
210
        return 0;
211
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.