Morten, Johannes und Angelo sind da. Der Code wird perfektioniert:
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <SDL/SDL.h> typedef struct _Color Color; struct _Color { unsigned char r, g, b; }; typedef struct _Point Point; struct _Point { int x, y; }; SDL_Surface *screen; void drawPixel(int x, int y, Color c) { *((Uint32*)((char*)screen->pixels + x*screen->format->BytesPerPixel + y*screen->pitch)) = SDL_MapRGB(screen->format, c.r, c.g, c.b); } void drawLine(Point p1, Point p2, Color c) { int dx, dy; int incx, incy; float m; dx = p2.x - p1.x; dy = p2.y - p1.y; incx = dx>0 ? 1 : -1; incy = dy>0 ? 1 : -1; if(dx == 0){ int y; for(y = p1.y; y != p2.y; y += incy) drawPixel(p1.x, y, c); return; } if(abs(dx)>abs(dy)){ int x; float y; m = (float)dy/(float)abs(dx); y = p1.y; for(x = p1.x; x != p2.x; x += incx){ drawPixel(x, y+0.5f, c); y += m; } }else{ int y; float x; m = (float)dx/(float)abs(dy); x = p1.x; for(y = p1.y; y != p2.y; y += incy){ drawPixel(x+0.5f, y, c); x += m; } } } void drawTriWire(Point p1, Point p2, Point p3, Color c) { drawLine(p1, p2, c); drawLine(p2, p3, c); drawLine(p3, p1, c); } void drawHorizontalLine(int y, float x1f, float x2f, Color c) { int x1, x2, tmp; x1 = (x1f == (int)x1f) ? x1f : x1f+1; x2 = (x2f == (int)x2f) ? x2f : x2f+1; if(x1 > x2){ tmp = x1; x1 = x2; x2 = tmp; } while(x1 < x2) drawPixel(x1++, y, c); } void //~ sortPoints(Point **p) ident mit zeile drunter, for educational purpose only sortPoints(Point *p[]) { #define SWAPPOINT(p,q) { \ tmp = p; \ p = q; \ q = tmp; \ } Point *tmp; if(p[0]->y > p[1]->y) SWAPPOINT(p[0], p[1]); if(p[0]->y > p[2]->y) SWAPPOINT(p[0], p[2]); if(p[1]->y > p[2]->y) SWAPPOINT(p[1], p[2]); #undef SWAPPOINT } void drawTriFlat(Point p1, Point p2, Point p3, Color c) { float dx[3]; int y; float x1, x2; Point *p[] = {&p1, &p2, &p3}; sortPoints(p); #define D(p,q) (q->y - p->y == 0 ? 0.0f : (float) (q->x - p->x) / (q->y - p->y)) //~ dx[0] = (float) (p[2]->x - p[0]->x) / (p[2]->y - p[0]->y); dx[0] = D(p[0], p[2]); dx[1] = D(p[0], p[1]); dx[2] = D(p[1], p[2]); #undef D y = p[0]->y; x1 = x2 = p[0]->x; while(y < p[1]->y){ drawHorizontalLine(y, x1, x2, c); y++; x1 += dx[0]; x2 += dx[1]; } x2 = p[1]->x; while(y < p[2]->y){ drawHorizontalLine(y, x1, x2, c); y++; x1 += dx[0]; x2 += dx[2]; } } void drawRect(Point p1, Point p2, Color rgb) { int i, j; for(i = p1.x; i < p2.x; i++) for(j = p1.y; j < p2.y; j++) drawPixel(i, j, rgb); } void draw(void) { drawRect((Point){40,140}, (Point){50,150}, (Color) {255, 0, 255}); drawRect((Point){80,140}, (Point){90,150}, (Color) {255, 0, 255}); drawRect((Point){62,160}, (Point){68,180}, (Color) {10, 123, 233}); drawRect((Point){40,195}, (Point){90,200}, (Color) {123, 233, 10}); // drawLine((Point){0, 0}, (Point){50, 30}); // drawLine((Point){0, 0}, (Point){30, 50}); // drawLine((Point){50, 0}, (Point){50, 50}); drawTriFlat((Point){30,120}, (Point){100, 120}, (Point){65, 100}, (Color){25, 85, 155}); } int main(void) { int running = 1; int w = 640; int h = 480; SDL_Event event; SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE); while(running){ while(SDL_PollEvent(&event)) if(event.type == SDL_QUIT) running = 0; SDL_LockSurface(screen); draw(); SDL_UnlockSurface(screen); SDL_UpdateRect(screen, 0, 0, 0, 0); } SDL_Quit(); return 0; }
bringt:
Angelo will keine Kreise machen.
#include #include #include #include <SDL/SDL.h> #include typedef struct _Color Color; struct _Color { unsigned char r, g, b; }; typedef struct _Point Point; struct _Point { int x, y; }; SDL_Surface *screen; void drawPixel(int x, int y, Color c) { *((Uint32*)((char*)screen->pixels + x*screen->format->BytesPerPixel + y*screen->pitch)) = SDL_MapRGB(screen->format, c.r, c.g, c.b); } void drawLine(Point p1, Point p2, Color c) { int dx, dy; int incx, incy; float m; dx = p2.x - p1.x; dy = p2.y - p1.y; incx = dx>0 ? 1 : -1; incy = dy>0 ? 1 : -1; if(dx == 0){ int y; for(y = p1.y; y != p2.y; y += incy) drawPixel(p1.x, y, c); return; } if(abs(dx)>abs(dy)){ int x; float y; m = (float)dy/(float)abs(dx); y = p1.y; for(x = p1.x; x != p2.x; x += incx){ drawPixel(x, y+0.5f, c); y += m; } }else{ int y; float x; m = (float)dx/(float)abs(dy); x = p1.x; for(y = p1.y; y != p2.y; y += incy){ drawPixel(x+0.5f, y, c); x += m; } } } void drawTriWire(Point p1, Point p2, Point p3, Color c) { drawLine(p1, p2, c); drawLine(p2, p3, c); drawLine(p3, p1, c); } void drawHorizontalLine(int y, float x1f, float x2f, Color c) { int x1, x2, tmp; x1 = (x1f == (int)x1f) ? x1f : x1f+1; x2 = (x2f == (int)x2f) ? x2f : x2f+1; if(x1 > x2){ tmp = x1; x1 = x2; x2 = tmp; } while(x1 < x2) drawPixel(x1++, y, c); } void //~ sortPoints(Point **p) ident mit zeile drunter, for educational purpose only sortPoints(Point *p[]) { #define SWAPPOINT(p,q) { \ tmp = p; \ p = q; \ q = tmp; \ } Point *tmp; if(p[0]->y > p[1]->y) SWAPPOINT(p[0], p[1]); if(p[0]->y > p[2]->y) SWAPPOINT(p[0], p[2]); if(p[1]->y > p[2]->y) SWAPPOINT(p[1], p[2]); #undef SWAPPOINT } void drawTriFlat(Point p1, Point p2, Point p3, Color c) { float dx[3]; int y; float x1, x2; Point *p[] = {&p1, &p2, &p3}; sortPoints(p); #define D(p,q) (q->y - p->y == 0 ? 0.0f : (float) (q->x - p->x) / (q->y - p->y)) //~ dx[0] = (float) (p[2]->x - p[0]->x) / (p[2]->y - p[0]->y); dx[0] = D(p[0], p[2]); dx[1] = D(p[0], p[1]); dx[2] = D(p[1], p[2]); #undef D y = p[0]->y; x1 = x2 = p[0]->x; while(y < p[1]->y){ drawHorizontalLine(y, x1, x2, c); y++; x1 += dx[0]; x2 += dx[1]; } x2 = p[1]->x; while(y < p[2]->y){ drawHorizontalLine(y, x1, x2, c); y++; x1 += dx[0]; x2 += dx[2]; } } void drawRect(Point p1, Point p2, Color rgb) { int i, j; for(i = p1.x; i < p2.x; i++) for(j = p1.y; j < p2.y; j++) drawPixel(i, j, rgb); } void drawCircle(Point center, int radius, Color c) { Point p; int x; int y; for(x = 0; x < radius; x++){ y = sqrt(radius*radius - x*x); drawLine((Point) {center.x + x, center.y + y}, (Point) {center.x + x, center.y - y}, c); drawLine((Point) {center.x - x, center.y + y}, (Point) {center.x - x, center.y - y}, c); } } void draw(void) { drawCircle((Point){65, 170}, 45, (Color) {128, 128, 0}); drawRect((Point){40,140}, (Point){50,150}, (Color) {255, 0, 255}); drawRect((Point){80,140}, (Point){90,150}, (Color) {255, 0, 255}); drawRect((Point){62,160}, (Point){68,180}, (Color) {10, 123, 233}); drawRect((Point){40,195}, (Point){90,200}, (Color) {123, 233, 10}); // drawLine((Point){0, 0}, (Point){50, 30}); // drawLine((Point){0, 0}, (Point){30, 50}); // drawLine((Point){50, 0}, (Point){50, 50}); drawTriFlat((Point){30,120}, (Point){100, 120}, (Point){65, 100}, (Color){25, 85, 155}); } int main(void) { int running = 1; int w = 640; int h = 480; SDL_Event event; SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE); while(running){ while(SDL_PollEvent(&event)) if(event.type == SDL_QUIT) running = 0; SDL_LockSurface(screen); draw(); SDL_UnlockSurface(screen); SDL_UpdateRect(screen, 0, 0, 0, 0); } SDL_Quit(); return 0; }
Nächste Woche ist nicht.
Angelo orientiert sich nach dem GS UsersManual für die PS2.