Ryan/Icculus, rtrussell, you have both been helpful but I’m still having some problems converting this from SDL1 to SDL2… it shows a window, then I press a key to continue, and it says ‘illegal instruction.’ I thought it was the getch() (a friend wrote) that was for SDL1 but if I leave that out, then the window just disappears and says the same… This code doesn’t work yet but if I can fix it, would be under same BSD license as my SDL1 code linked in original post. The same graphics worked in the original, and I know I could probably do it all with more advanced SDL functions and/or OpenGL but the point is learning the mathematics, then continuing to do so by writing more C functions… I’m not sure it’ll work to do it the same sort of way as with SDL1. Sometimes I put some pixels/lines, waited for a key-press, put some more pixels/lines, and sometimes I erased it after every drawing of a cube… so unsure which of the two ways (rtrussell mentioned) I should be doing this and if that’s the issue… The reason I wrote put_pixel() & line() functions is the earlier code used to compile for a couple compilers so I used #define & #ifdef.
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h> //not needed for SDL2?
#include <SDL2/SDL_render.h>
#include <math.h> //ceil(), cos(), floor(), sin(), sqrt()
#include <stdio.h> //printf()
//#include <stdlib.h> //abs()
void clrscr(SDL_Renderer *renderer,SDL_Color colour);
void delay(int n);
void getch(void);
void put_pixel(SDL_Renderer *renderer,int x,int y,SDL_Color colour);
void line(SDL_Renderer *renderer,int x0,int y0,int x1,int y1,SDL_Color colour);
void draw_square(SDL_Renderer *renderer,int square[][4],SDL_Color colour);
void draw_cube(SDL_Renderer *renderer,int cube[][8],int z,SDL_Color colour);
int main(void)
{
int i;
//define polygons
int square_100[2][4]={{50, 50, -50, -50},
{50, -50, -50, 50}};
int square_a[2][4]={{50, 50, -50, -50},
{50, -50, -50, 50}};
//define polyhedra
int cube_100[3][8]={{-50, -50, 50, 50, -50, -50, 50, 50},
{-50, 50, 50, -50, -50, 50, 50, -50},
{-50, -50, -50, -50, 50, 50, 50, 50}};
int cube_a[3][8]={{-50, -50, 50, 50, -50, -50, 50, 50},
{-50, 50, 50, -50, -50, 50, 50, -50},
{-50, -50, -50, -50, 50, 50, 50, 50}};
float x,y,z;
//define 3D angles and rotation matrices
//alpha, beta, lambda
//float a=0;
float a=0.785398; //pi/4;
//float b=0;
float b=0.785398;
float l=0.785398;
float ml[2][2]={{ cos(l), sin(l)},
{-sin(l), cos(l)}};
float mR3a[3][3]={{1, 0, 0},
{0, cos(a), -sin(a)},
{0, sin(a), cos(a)}};
float mR3b[3][3]={{ cos(b), 0, sin(b)},
{ 0, 1, 0},
{-sin(b), 0, cos(b)}};
float mR3l[3][3]={{cos(l), -sin(l), 0},
{sin(l), cos(l), 0},
{ 0, 0, 1}};
float mR3r[3][3]={{ cos(b)*cos(l)+sin(a)*sin(b)*sin(l), -cos(a)*sin(l)+cos(l)*sin(a)*sin(b), cos(a)*sin(b)},
{ cos(a)*sin(l), cos(a)*cos(l), -sin(a)},
{-cos(l)*sin(b)+cos(b)*sin(a)*sin(l), sin(b)*sin(l)+cos(b)*cos(l)*sin(a), cos(a)*cos(b)}};
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Color black={0x00,0x00,0x00,0x00};
SDL_Color green={0x00,0xFF,0x00,0x00};
for(i=0;i<=3;i++)
{
square_a[0][i]+=320;
square_a[1][i]+=240;
}
if (SDL_Init(SDL_INIT_VIDEO)<0)
{
fprintf(stderr,"Cannot start SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
window=SDL_CreateWindow("demo",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,0);
if (window==NULL)
{
fprintf(stderr,"Cannot set 640x480x256 video mode: %s\n", SDL_GetError());
return 1;
}
renderer=SDL_CreateRenderer(window,-1,0);
clrscr(renderer,black);
SDL_RenderPresent(renderer);
getch();
put_pixel(renderer,320,240,green);
SDL_RenderPresent(renderer);
getch();
draw_square(renderer,square_a,green);
SDL_RenderPresent(renderer);
getch();
//rotation
//pi/24=0.130899
//pi/30=0.104719
//pi/60=0.052360 optimal at 640*480
for(l=0;l<=6.283185;l+=0.104719)
{
//square rotation matrices, defining trigonometric functions
ml[0][0]=cos(l);
ml[0][1]=sin(l);
ml[1][0]=-ml[0][1];
ml[1][1]=ml[0][0];
for(i=0; i<=3; i++)
{
//printf("%s%d%s%d%s%d\n", "old quad: ", i+1, "x,y: ", square_100[0][i], " ", square_100[1][i], "\n");
x=ml[0][0]*square_100[0][i]+ml[0][1]*square_100[1][i];
y=ml[1][0]*square_100[0][i]+ml[1][1]*square_100[1][i];
square_a[0][i]=(int)x;
square_a[1][i]=(int)y;
//printf("%s%d%s%d%s%d\n", "new quad: ", i+1, "x,y: ", square_100[0][i], " ", square_100[1][i], "\n");
}
for(i=0;i<=3;i++)
{
square_a[0][i]+=320;
square_a[1][i]+=240;
}
delay(10);
clrscr(renderer,black);
draw_square(renderer,square_a,green);
SDL_RenderPresent(renderer);
}
getch();
clrscr(renderer, black);
draw_cube(renderer,cube_a,256,green);
SDL_RenderPresent(renderer);
getch();
for(l=0;l<=6.283185;l+=0.104719)
{
mR3r[0][0]=cos(b)*cos(l)+sin(a)*sin(b)*sin(l);
mR3r[0][1]=-cos(a)*sin(l)+cos(l)*sin(a)*sin(b);
mR3r[0][2]=cos(a)*sin(b);
mR3r[1][0]=cos(a)*sin(l);
mR3r[1][1]=cos(a)*cos(l);
mR3r[1][2]=-sin(a);
mR3r[2][0]=-cos(l)*sin(b)+cos(b)*sin(a)*sin(l);
mR3r[2][1]=sin(b)*sin(l)+cos(b)*cos(l)*sin(a);
mR3r[2][2]=cos(a)*cos(b);
for(i=0; i<=7; i++)
{
x=mR3r[0][0]*cube_100[0][i]+mR3r[0][1]*cube_100[1][i]+mR3r[0][2]*cube_100[2][i];
y=mR3r[1][0]*cube_100[0][i]+mR3r[1][1]*cube_100[1][i]+mR3r[1][2]*cube_100[2][i];
z=mR3r[2][0]*cube_100[0][i]+mR3r[2][1]*cube_100[1][i]+mR3r[2][2]*cube_100[2][i];
cube_a[0][i]=(int)x;
cube_a[1][i]=(int)y;
cube_a[2][i]=(int)z;
}
delay(10);
clrscr(renderer,black);
draw_cube(renderer,cube_a,256,green);
SDL_RenderPresent(renderer);
}
//wait; set video mode to text; exit
getch();
printf("%s","Programmed by David Chmelik and Ben Collver. (http://www.cwu.edu/~melikd/, http://www.terrorpin.net/\n");
return 0;
}
void clrscr(SDL_Renderer *renderer,SDL_Color colour)
{
SDL_SetRenderDrawColor(renderer,colour.r,colour.g,colour.b,colour.a);
SDL_RenderClear(renderer);
}
void delay(int n)
{
SDL_Delay(n);
}
void getch(void)
{
SDL_Event event;
int done=0;
while(!done){
if(SDL_WaitEvent(&event)<0)
{
fprintf(stderr, "SDL_WaitEvent() error: %s\n",
SDL_GetError());
done=1;
continue;
}
if (event.type == SDL_KEYUP) done=1;
}
}
void put_pixel(SDL_Renderer *renderer, int x, int y, SDL_Color colour)
{
SDL_SetRenderDrawColor(renderer,colour.r,colour.g,colour.b,colour.a);
SDL_RenderDrawPoint(renderer,x,y);
}
void line(SDL_Renderer *renderer, int x0, int y0, int x1, int y1, SDL_Color colour)
{
SDL_SetRenderDrawColor(renderer,colour.r,colour.g,colour.b,colour.a);
SDL_RenderDrawLine(renderer,x0,y0,x1,y1);
}
void draw_square(SDL_Renderer *renderer,int square[][4],SDL_Color colour)
{
SDL_SetRenderDrawColor(renderer,colour.r,colour.g,colour.b,colour.a);
line(renderer,square[0][0],square[1][0],square[0][1],square[1][1], colour);
line(renderer,square[0][1],square[1][1],square[0][2],square[1][2], colour);
line(renderer,square[0][2],square[1][2],square[0][3],square[1][3], colour);
line(renderer,square[0][3],square[1][3],square[0][0],square[1][0], colour);
}
void draw_cube(SDL_Renderer *renderer, int cube[][8], int z, SDL_Color colour)
{
line(renderer,z*cube[0][0]/(z+cube[2][0])+320,z*cube[1][0]/(z+cube[2][0])+240,z*cube[0][1]/(z+cube[2][1])+320,z*cube[1][1]/(z+cube[2][1])+240, colour);
line(renderer,z*cube[0][0]/(z+cube[2][0])+320,z*cube[1][0]/(z+cube[2][0])+240,z*cube[0][3]/(z+cube[2][3])+320,z*cube[1][3]/(z+cube[2][3])+240, colour);
line(renderer,z*cube[0][0]/(z+cube[2][0])+320,z*cube[1][0]/(z+cube[2][0])+240,z*cube[0][4]/(z+cube[2][4])+320,z*cube[1][4]/(z+cube[2][4])+240, colour);
line(renderer,z*cube[0][1]/(z+cube[2][1])+320,z*cube[1][1]/(z+cube[2][1])+240,z*cube[0][2]/(z+cube[2][2])+320,z*cube[1][2]/(z+cube[2][2])+240, colour);
line(renderer,z*cube[0][1]/(z+cube[2][1])+320,z*cube[1][1]/(z+cube[2][1])+240,z*cube[0][5]/(z+cube[2][5])+320,z*cube[1][5]/(z+cube[2][5])+240, colour);
line(renderer,z*cube[0][2]/(z+cube[2][2])+320,z*cube[1][2]/(z+cube[2][2])+240,z*cube[0][6]/(z+cube[2][6])+320,z*cube[1][6]/(z+cube[2][6])+240, colour);
line(renderer,z*cube[0][3]/(z+cube[2][3])+320,z*cube[1][3]/(z+cube[2][3])+240,z*cube[0][2]/(z+cube[2][2])+320,z*cube[1][2]/(z+cube[2][2])+240, colour);
line(renderer,z*cube[0][3]/(z+cube[2][3])+320,z*cube[1][3]/(z+cube[2][3])+240,z*cube[0][7]/(z+cube[2][7])+320,z*cube[1][7]/(z+cube[2][7])+240, colour);
line(renderer,z*cube[0][4]/(z+cube[2][4])+320,z*cube[1][4]/(z+cube[2][4])+240,z*cube[0][7]/(z+cube[2][7])+320,z*cube[1][7]/(z+cube[2][7])+240, colour);
line(renderer,z*cube[0][5]/(z+cube[2][5])+320,z*cube[1][5]/(z+cube[2][5])+240,z*cube[0][6]/(z+cube[2][6])+320,z*cube[1][6]/(z+cube[2][6])+240, colour);
line(renderer,z*cube[0][5]/(z+cube[2][5])+320,z*cube[1][5]/(z+cube[2][5])+240,z*cube[0][4]/(z+cube[2][4])+320,z*cube[1][4]/(z+cube[2][4])+240, colour);
line(renderer,z*cube[0][7]/(z+cube[2][7])+320,z*cube[1][7]/(z+cube[2][7])+240,z*cube[0][6]/(z+cube[2][6])+320,z*cube[1][6]/(z+cube[2][6])+240, colour);
}