Computer-Ag am WvS Blog der Computer-AG am Werner von Siemens Gymnasium Berlin

21. Januar 2015

21.1.2015

Morten ist da. Wir reden über Windows ultimate. Und Ableton.

Jetzt sieht der Code so aus:

#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
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,40}, (Point){50,50}, (Color) {255, 0, 255});
    drawRect((Point){80,40}, (Point){90,50}, (Color) {255, 0, 255});
    drawRect((Point){62,60}, (Point){68,80}, (Color) {10, 123, 233});
    drawRect((Point){40,95}, (Point){90,100}, (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});
        drawTriWire((Point){10,10}, (Point){100, 70}, (Point){30, 150}, (Color){255, 255, 255});
}

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;
}

 

Und das Resutlat so:

 

 

c2

Nächste Mal dann Ausfüllen von Dreiecken, 3D-Rotation und Kreise …;

Morten bastelt daran rum, wie ein Spiel unter Windows startet …;

Diesen Freitag ist noch einmal, dann zwei Wochen Pause.

 

Keine Kommentare »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment