Original Post
My question today is, "How to rotate an image with SDL?". I've tried SDL_Gfx's rotozoom, but I'd like to make my own function for that. So I started to study math, and I have accomplished a code like this: No comments, sorry. :-) But the code above rotates a corner (two lines), and my goal with that code was to rotate a full square, and then texturise it with lines. Yes, I know, it sounds stupid, but it was the first thing that came to my mind. So any ideas to rotate a texturised image? Or to improve my code above? Thanks already! -nyrhinen
#include <math.h>
#include <SDL/SDL.h>
#define PI 3.14159
#define SWIDTH 640
#define SHEIGHT 400
void Putpixel(SDL_Surface *screen, int x, int y, int r, int g, int b) {
unsigned char *pixel;
pixel = screen->pixels;
pixel = pixel + y * SWIDTH * 4;
pixel = pixel + x * 4;
*pixel = b; pixel++;
*pixel = g; pixel++;
*pixel = r;
}
int sgn (long a) {
if (a > 0) return +1;
else if (a < 0) return -1;
else return 0;
}
void Line(SDL_Surface *screen, int a, int b, int c, int d, int col) {
long u,s,v,d1x,d1y,d2x,d2y,m,n;
int i;
u = c-a;
v = d-b;
d1x = sgn(u);
d1y = sgn(v);
d2x = sgn(u);
d2y = 0;
m = abs(u);
n = abs(v);
if (m <= n) {
d2x = 0;
d2y = sgn(v);
m = abs(v);
n = abs(u);
}
s = (int)(m / 2);
for (i = 0; i < round(m); i++) {
Putpixel(screen, a, b, 255, 255, 255);
s = s + n;
if (s >= m) {
s = s - m;
a = a + d1x;
b = b + d1y;
} else {
a = a + d2x;
b = b + d2y;
}
}
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Rotating", NULL);
SDL_Surface *screen = SDL_SetVideoMode(640, 400, 32, SDL_SWSURFACE);
SDL_Event event;
int angle = 0, i;
while (1) {
SDL_FillRect(screen, NULL, 0);
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) break;
else if (event.type == SDL_KEYDOWN)
if (event.key.keysym.sym == SDLK_ESCAPE) break;
Line(screen, 320, 200, -sin(angle*PI/180)*100+320, cos(angle*PI/180)*100+200, 255);
Line(screen, 320, 200, -sin((angle+90)*PI/180)*100+320, cos((angle+90)*PI/180)*100+200, 255);
if (angle > 360) angle = 0;
angle = angle + 1;
SDL_Delay(1);
SDL_Flip(screen);
}
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}