1 /************************************************************************
2  * This file is part of Wizznic.                                        *
3  * Copyright 2009-2015 Jimmy Christensen <dusted@dusted.dk>             *
4  * Wizznic is free software: you can redistribute it and/or modify      *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation, either version 3 of the License, or    *
7  * (at your option) any later version.                                  *
8  *                                                                      *
9  * Wizznic is distributed in the hope that it will be useful,           *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
12  * GNU General Public License for more details.                         *
13  *                                                                      *
14  * You should have received a copy of the GNU General Public License    *
15  * along with Wizznic.  If not, see <http://www.gnu.org/licenses/>.     *
16  ************************************************************************/
17 
18 #include "levelselector.h"
19 #include <math.h>
20 #include "text.h"
21 #include "pixel.h"
22 #include "ticks.h"
23 #include "pack.h"
24 #include "defs.h"
25 #include "settings.h"
26 
27 static int lastSelected = -1;
28 static SDL_Surface* lvlPreviewImg=0;
29 static char buf[128];
30 static char buf2[128];
31 static char buf3[128];
32 
33 
resetLevelSelector()34 void resetLevelSelector()
35 {
36   lastSelected=-1;
37   if(lvlPreviewImg)
38   {
39     SDL_FreeSurface(lvlPreviewImg);
40     lvlPreviewImg=0;
41   }
42 }
43 
levelSelector(SDL_Surface * screen,int l,int stats)44 void levelSelector(SDL_Surface* screen, int l, int stats)
45 {
46 
47   if(lastSelected != l)
48   {
49     //Unload the previously loaded level-preview
50     if(lastSelected != -1)
51     {
52       SDL_FreeSurface(lvlPreviewImg);
53     }
54     //Load the level-preview for the currently selected image
55     //printf("Loading file: %s\n",levelInfo(l)->imgFile);
56     lvlPreviewImg=loadImg(levelInfo(l)->imgFile);
57 
58     //If it wasen't found, load the "No image" image
59     if(!lvlPreviewImg) lvlPreviewImg=loadImg("data/nolvlimg.png");
60 
61     lastSelected=l;
62     sprintf(buf, "Level %i", l);
63     sprintf(buf2, "Time %i:%1.2i", levelInfo(l)->time/60, levelInfo(l)->time%60 );
64     sprintf(buf3,"By: %s", levelInfo(l)->author);
65   }
66 
67   if(l+1 > getNumLevels()) stats=2;
68 
69   drawPreviewImg(screen, lvlPreviewImg, stats);
70 
71   if(stats!=2)
72   {
73 
74     if(stats)
75     {
76       txtWriteCenter(screen, FONTMEDIUM, buf, HSCREENW,HSCREENH-22 );
77       txtWriteCenter(screen, FONTSMALL, levelInfo(l)->levelName, HSCREENW,HSCREENH);
78       txtWriteCenter(screen, FONTSMALL, buf3, HSCREENW,HSCREENH+12 );
79       txtWriteCenter(screen, FONTSMALL, buf2, HSCREENW,HSCREENH+36 );
80     } else {
81       txtWriteCenter(screen, FONTSMALL, "Locked!", HSCREENW,HSCREENH);
82     }
83   }
84 }
85 
86 
87 
88 static float rot=0.0;
drawPreviewImg(SDL_Surface * screen,SDL_Surface * img,int stats)89 void drawPreviewImg(SDL_Surface* screen, SDL_Surface* img, int stats)
90 {
91   int x, y; //In the source image
92   int offSetX=HSCREENW-(55*2);
93   int offSetY=HSCREENH-(55*2);
94   int nx, ny; //new x/y value for px
95   uint32_t col; //Color of pixel
96   uint8_t r,g,b;
97   uint32_t grey;
98 
99   float pxInc = 6.28318531/110.0;
100 
101   float xInc;
102 
103   rot-=(float)getTicks()/200;
104 
105 
106   for(y=0; y < 110; y++)
107   {
108     xInc = round(cos(rot+y*pxInc)*10);
109 
110     for(x=0; x < 110; x++)
111     {
112       col = freadPixel(img, x, y );
113       //Do expensive colorkeying
114       r = ((col & img->format->Rmask) >> img->format->Rshift);
115       g = ((col & img->format->Gmask) >> img->format->Gshift);
116       b = ((col & img->format->Bmask) >> img->format->Bshift);
117 
118       if( !isAlpha(r,g,b) )
119       {
120 
121         //Do b/w
122         if(!stats)
123         {
124           grey = (r+g+b)/3;
125           col = (grey << img->format->Rshift) | (grey << img->format->Gshift)<<((setting()->bpp==2)?1:0) | (grey << img->format->Bshift);
126         }
127 
128         nx = x*2;
129         ny = y*2;
130         nx += xInc;
131 
132         plotPixel(screen, nx+offSetX,ny+offSetY, col);
133       }
134     }
135   }
136 }
137