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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#define DIM_H 800
#define DIM_V 600
#define BUTTON_SX 1
#define ROWS 6
//dimensions des cases
#define ISO_WIDTH 128
#define ISO_HEIGHT 64
//sin(-45°) & cos(-45°)
#define SIN_MIN_45 -0.707106
#define COS_MIN_45 0.707106
//sqrt(2)
#define SQRT2 1.414213
typedef struct point
{
int x;
int y;
} point;
typedef struct float_point
{
float x;
float y;
} f_point;
typedef struct RGB
{
Uint8 R;
Uint8 G;
Uint8 B;
} RGB_Color;
//fonctions
void draw_pixel(SDL_Surface *surface, int x, int y, RGB_Color s_color);
void draw_cross(SDL_Surface *surface, point p, RGB_Color c_color);
void draw_line(SDL_Surface *surface, point p0, point p1, RGB_Color color);
void draw_iso_square(SDL_Surface *surface,point p, int len, RGB_Color color);
SDL_Surface * init_img(char img[], RGB_Color trans);
f_point * iso2scr(point scr, point start);
int main(int argc, char *argv[])
{
SDL_Surface *screen;
SDL_Surface *screen_backup;
SDL_Surface *cursor;
SDL_Surface *bg_cursor;
RGB_Color fuchsia = {0xFF,0x0,0xFF};
RGB_Color red = {0xFF,0x0,0x0};
RGB_Color yellow = {0xFF,0xFF,0x0};
RGB_Color white = {0xFF,0xFF,0xFF};
if( SDL_Init(SDL_INIT_VIDEO) <0 )
{
printf("SDL Error: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
if(!( screen = SDL_SetVideoMode(DIM_H, DIM_V, 0,flags) ))
{
printf("SDL error: %s\n", SDL_GetError());
return 1;
}
//on desactive le curseur original
SDL_ShowCursor(0);
//initialisation de l'image du curseur
cursor = init_img("img/m_pointer.bmp",fuchsia);
flags = SDL_HWSURFACE|SDL_SRCCOLORKEY;
bg_cursor=SDL_CreateRGBSurface(flags,cursor->w,cursor->h,cursor->format->BitsPerPixel,0,0,0,0);
screen_backup=SDL_CreateRGBSurface(flags,screen->w,screen->h,screen->format->BitsPerPixel,0,0,0,0);
int done = 0;
SDL_Rect rect_cur;
rect_cur.x = 0;
rect_cur.y = 0;
rect_cur.w = cursor->w;
rect_cur.h = cursor->h;
//le point où le premier carré iso est déssiné
point start;
//point temporaire
point p;
start.x = (DIM_H - ISO_WIDTH)/2;
start.y = 0;
//taille d'un coté d'un carré iso
float iso_side = (float) (ISO_HEIGHT * SQRT2);
//DEBUG
printf("ISO_SIDE: %f\n\n", iso_side);
int k,z;
//la grille
for(k = 0; k < ROWS; k++)
{
for(z = 0; z < ROWS; z++)
{
//conversion des coordonnées de l'écran en coordonées iso
p.x = (z-k)*(ISO_HEIGHT)+start.x;
p.y = (z+k)*(ISO_HEIGHT/2)+start.y;
//dessine un carré iso
draw_iso_square(screen,p,ISO_WIDTH,white);
}
}
SDL_Flip(screen);
//coordonnées de l'écran
point s_cord;
//iso (0,0)
point i_start;
i_start.x=start.x+ISO_HEIGHT;
i_start.y=start.y;
int row, col, old_row=0,old_col=0;
SDL_Event event;
f_point * iso;
while(!done)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
done = 1;
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
if (event.key.keysym.sym == SDLK_SPACE)
{
SDL_BlitSurface(screen,NULL,screen_backup,NULL);
flags ^= SDL_FULLSCREEN;
screen = SDL_SetVideoMode(DIM_H, DIM_V, 0,flags);
SDL_BlitSurface(screen_backup,NULL,screen,NULL);
}
}
if(event.button.button == BUTTON_SX && event.button.type == SDL_MOUSEBUTTONDOWN)
{
s_cord.x=event.button.x;
s_cord.y=event.button.y;
//DEBUG
printf("ISO: x = %d | y = %d\n",s_cord.x,s_cord.y);
draw_cross(screen,s_cord,yellow);
iso = iso2scr(s_cord,i_start);
row = (int)(iso->x/iso_side);
col = (int)(iso->y/iso_side);
if(iso->x < 0 & iso->x >= -iso_side)
row = -1;
if(iso->y < 0 & iso->y >= -iso_side)
col = -1;
//DEBUG
printf("ORTHO: x = %f | y = %f\n",iso->x,iso->y);
printf("CELL: (%d,%d)\n\n",row,col);
free(iso);
}
if(event.motion.type == SDL_MOUSEMOTION)
{
rect_cur.x = event.button.x;
rect_cur.y = event.button.y;
rect_cur.w = cursor->w;
rect_cur.h = cursor->h;
s_cord.x=event.button.x;
s_cord.y=event.button.y;
iso = iso2scr(s_cord,i_start);
row = (int)(iso->x/iso_side);
col = (int)(iso->y/iso_side);
if(iso->x < 0 & iso->x >= -iso_side)
row = -1;
if(iso->y < 0 & iso->y >= -iso_side)
col = -1;
if((row!=old_row || col!=old_col))
{
if(old_row >= 0 & old_col >= 0 & old_row < ROWS & old_col < ROWS)
{
p.x = (old_row-old_col)*(ISO_HEIGHT)+start.x;
p.y = (old_row+old_col)*(ISO_HEIGHT/2)+start.y;
draw_iso_square(screen,p,ISO_WIDTH,white);
}
if(row >= 0 & col >= 0 & row < ROWS & col < ROWS)
{
p.x = (row-col)*(ISO_HEIGHT)+start.x;
p.y = (row+col)*(ISO_HEIGHT/2)+start.y;
draw_iso_square(screen,p,ISO_WIDTH,red);
}
old_row = row;
old_col = col;
}
free(iso);
}
}
SDL_BlitSurface(screen, &rect_cur, bg_cursor, NULL);
SDL_BlitSurface(cursor, NULL, screen, &rect_cur);
SDL_Flip(screen);
SDL_BlitSurface(bg_cursor, NULL, screen, &rect_cur);
SDL_Delay(1);
}
return 0;
}
void draw_pixel(SDL_Surface *surface, int x, int y, RGB_Color s_color)
{
Uint32 color = SDL_MapRGB(surface->format, s_color.R, s_color.G, s_color.B);
int bpp = surface->format->BytesPerPixel;
Uint8 *pixel = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch (bpp)
{
case 1: // 1 byte => 8-bpp
*pixel = color;
break;
case 2: // 2 byte => 16-bpp
*(Uint16 *)pixel = color;
break;
case 3: // 3 byte => 24-bpp
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
pixel[0] = (color >> 16) & 0xff;
pixel[1] = (color >> 8) & 0xff;
pixel[2] = color & 0xff;
}
else
{
pixel[0] = color & 0xff;
pixel[1] = (color >> 8) & 0xff;
pixel[2] = (color >> 16) & 0xff;
}
break;
case 4: // 4 byte => 32-bpp
*(Uint32 *)pixel = color;
break;
}
}
void draw_cross(SDL_Surface *surface, point p, RGB_Color c_color)
{
if ( SDL_MUSTLOCK(surface) )
SDL_LockSurface(surface);
draw_pixel(surface,p.x,p.y,c_color);
draw_pixel(surface,p.x+1,p.y,c_color);
draw_pixel(surface,p.x-1,p.y,c_color);
draw_pixel(surface,p.x,p.y+1,c_color);
draw_pixel(surface,p.x,p.y-1,c_color);
if ( SDL_MUSTLOCK(surface) )
SDL_UnlockSurface(surface);
}
void draw_line(SDL_Surface *surface,point p0, point p1, RGB_Color color)
{
int i;
int comp;
int d_a,d_b;
int a,b;
int inc;
if ( SDL_MUSTLOCK(surface) )
SDL_LockSurface(surface);
if(p0.x==p1.x && p0.y==p1.y)
draw_pixel(surface,p0.x,p0.y,color);
else if(abs(p1.x-p0.x)>=abs(p1.y-p0.y))
{
d_a=p1.x-p0.x;
d_b=p1.y-p0.y;
a=p0.x;
b=p1.x;
if(p0.x<p1.x)
inc=1;
else
inc=-1;
b+=inc;
for(i=a;i!=b;i=i+inc)
{
comp=(d_b*(i-a))/(d_a)+p0.y;
draw_pixel(surface,i,comp,color);
}
}
else
{
d_a=p1.y-p0.y;
d_b=p1.x-p0.x;
a=p0.y;
b=p1.y;
if(p0.y<p1.y)
inc=1;
else
inc=-1;
b+=inc;
for(i=a;i!=b;i=i+inc)
{
comp=(d_b*(i-a))/(d_a)+p0.x;
draw_pixel(surface,comp,i,color);
}
}
if ( SDL_MUSTLOCK(surface) )
SDL_UnlockSurface(surface);
}
void draw_iso_square(SDL_Surface *surface,point p, int len, RGB_Color color)
{
point temp;
temp.x=p.x+(len/2);
temp.y=p.y;
p.y+=(len/4);
draw_line(surface,p,temp,color);
temp.y+=(len/2);
draw_line(surface,p,temp,color);
temp.y-=(len/2);
p.x+=len;
draw_line(surface,p,temp,color);
temp.y+=(len/2);
draw_line(surface,p,temp,color);
}
SDL_Surface * init_img(char img[], RGB_Color trans)
{
SDL_Surface *good;
SDL_Surface *temp;
if((temp = SDL_LoadBMP(img))==NULL)
{
printf("SDL error (init_img): %s\n",IMG_GetError());
exit(1);
}
good = SDL_DisplayFormat(temp);
SDL_SetColorKey(good, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(good->format, trans.R, trans.G, trans.B));
SDL_FreeSurface(temp);
return good;
}
f_point * iso2scr(point scr, point start)
{
f_point * iso = (f_point *)malloc(sizeof(point));
scr.x -= start.x;
scr.y -= start.y;
scr.y *= 2;
iso->x = (scr.x*COS_MIN_45-scr.y*SIN_MIN_45);
iso->y = (scr.x*SIN_MIN_45+scr.y*COS_MIN_45);
return iso;
} |