tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
0
ZUGRIFFE
301
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
  1. #1
    OnlyFoo OnlyFoo ist offline Mitglied Brokat
    Registriert seit
    Feb 2005
    Beiträge
    470
    Dieses Mal ist meine Lösung nicht so ausführlich, aber rfunktionsfähig

    Die Rand-Zellen sind bei mir tote Zellen, so erspar ich mir das Herumgeprüfe, wieviele Nachbarzellen eine Zelle hat.

    Die Größe des Feldes ist fix, das Eingabe-Format für die Initialbelegung wie in der Aufgabenstellung vorgeschlagen.

    Hier gibts ein Video von einer Animation:
    http://keller-delirium.de/~olli/videos/quiz7.avi

    Und hier den Quellcode (SDL wird benötigt)

    Code c:
    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
    
    #include <SDL.h>
    #include <stdio.h>
    #include <stdlib.h>
     
    #define CELL_SIZE 4
    #define WORLD_WIDTH (640 / 4)
    #define WORLD_HEIGHT (480 / 4)
    #define STEPPING 1
     
    #define ALIVE 255
    #define DEAD 0
     
    /* Typ einer Zelle */
    typedef unsigned char cell_type;
     
    /* Bildschirm zum drauf zeichnen */
    SDL_Surface *screen;
     
    /* Array mit den Zellen, zwei Versionen */
    cell_type *world, *next_world;
     
    /* Initialisiert SDL quick&dirty */
    int sdl_init() {
        SDL_Init( SDL_INIT_VIDEO );
        
        screen = SDL_SetVideoMode( WORLD_WIDTH * CELL_SIZE,
            WORLD_HEIGHT * CELL_SIZE, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
            
        return screen != NULL;
    }
     
    /* Zeichnet width * height Zellen */
    void draw_world() {
        int x, y, i;
        cell_type *world_row;
        unsigned char *pixel_data, *pixel_row;
        
        SDL_LockSurface( screen );
        
        pixel_data = (unsigned char*)screen->pixels;
        
        for( y = 1; y < WORLD_HEIGHT - 1; y++ ) {
            world_row = world + y * WORLD_WIDTH;
            pixel_row = pixel_data + y * CELL_SIZE * screen->pitch;
            
            /* zeichne eine Zeile */
            for( x = 1; x < WORLD_WIDTH - 1; x++ ) {
                for( i = 0; i < CELL_SIZE; i++ ) {
                
                    /* Zeichne den Pixel */
                    *(pixel_row + 4 * (x * CELL_SIZE + i) + 0 ) =
                        *(pixel_row + 4 * (x * CELL_SIZE + i) + 1) =
                        *(pixel_row + 4 * (x * CELL_SIZE + i) + 2) = *(world_row + x);
                }   
            }
            
            /* kopiere die Zeile ein paar mal */
            for( i = 1; i < CELL_SIZE; i++ ) {
                memcpy( pixel_row + i * screen->pitch, pixel_row, screen->pitch );
            }
        }
        
        SDL_UnlockSurface( screen );
    }
     
    /* Initialisiert die Welt */
    void initialize_world() {
        char line[ 1024 ];
        int y = 1, x;
        
        /* erzeuge die zwei Welten */
        world = (cell_type*)malloc(
            WORLD_WIDTH * WORLD_HEIGHT * sizeof( cell_type ) );
            
        next_world = (cell_type*)malloc(
            WORLD_WIDTH * WORLD_HEIGHT * sizeof( cell_type ) );
        
        /* rotte beide Welten aus */
        memset( world, DEAD,
            WORLD_WIDTH * WORLD_HEIGHT * sizeof( cell_type ) );
            
        memset( next_world, DEAD,
            WORLD_WIDTH * WORLD_HEIGHT * sizeof( cell_type ) );
        
        while( fgets( line, sizeof( line ), stdin ) && y < WORLD_HEIGHT - 1 ) {
            /* Kommentare überspringen */
            if( *line == '#' )
                continue;
            
            /* gehe die Zeile durch, setzte ein
                lebendiges Vieh für jedes o */
            for( x = 1; x < WORLD_WIDTH - 1 && x < sizeof( line ) && line[x - 1] >= 32; x++ ) {
                if( line[x - 1] == 'o' )
                    *( world + y * WORLD_WIDTH + x ) = 255;
            }
            
            y++;
        }
    }
     
     
    /* simuliert einen Schritt */
    void simulate() {
        int x, y, sum;
        cell_type *rows[3], *next_world_row, *temp;
        
        /* Kopiere aktuelle Welt */
        memcpy( next_world, world,
            WORLD_WIDTH * WORLD_HEIGHT * sizeof( cell_type ) );
        
        /* Rand ist tod */
        for( y = 1; y < WORLD_HEIGHT - 1; y++ ) {
            rows[0] = world + (y - 1) * WORLD_WIDTH;
            rows[1] = world + y * WORLD_WIDTH;
            rows[2] = world + (y + 1) * WORLD_WIDTH;
            
            next_world_row = next_world + y * WORLD_WIDTH;
            
            for( x = 1; x < WORLD_WIDTH - 1; x++ ) {
                /* Suche Nachbarn */
                sum = (rows[0][x - 1] + rows[0][x] + rows[0][x + 1]
                     + rows[1][x - 1] +            + rows[1][x + 1]
                     + rows[2][x - 1] + rows[2][x] + rows[2][x + 1]) / ALIVE;
                
                /* Wecke oder töte Zelle */
                if( rows[1][x] == ALIVE ) {
                    if( sum < 2 || sum > 3 ) {
                        next_world_row[x] = DEAD;
                    }
                } else {
                    if( sum == 3 ) {
                        next_world_row[x] = ALIVE;
                    }
                }
                
            }
        }
        
        /* Wechsel die Welten */
        temp = next_world;
        next_world = world;
        world = temp;
    }
     
    int main( int argc, const char *argv[] ) {
        SDL_Event event;
        int running = 1, pause = 1, frame, i, timer, steps;
        /*
        char buffer[ 1024 ];
        */
        
        initialize_world();
        
        if( ! sdl_init() )
            return 1;
        
        frame = 0;
        steps = 0;
        timer = SDL_GetTicks();
        while( running ) {
            /* Fange Events ab */
            while( SDL_PollEvent( &event ) ) {
                if( event.type == SDL_QUIT )
                    running = 0;
                
                if( event.type == SDL_KEYDOWN )
                    pause = ! pause;
            }
            
            /* Simuliere, wenn wir nicht auf Pause sind */
            if( !pause ) {
                for( i = 0; i < STEPPING; i++ ) {
                    steps++;
                    simulate();
                }
            }
            
            /* Zeichne die Welt */
            draw_world();
            SDL_Flip( screen );
            
            /* Speichere jeden Frame */
            /*
            sprintf( buffer, "./frame%08d.bmp", frame );
            SDL_SaveBMP( screen, buffer );
            frame++;
            */
            
            /* gebe die Geschwindigkeit aus */
            if( steps >= 1000 ) {
                printf( "%d steps in %dms = %1.2ffps\n",
                    steps,
                    SDL_GetTicks() - timer,
                    (double)steps / ((SDL_GetTicks() - timer) * 0.001f) );
                
                steps = 0;
                timer = SDL_GetTicks();
            }
        }
        
        SDL_Quit();
        return 0;
    }
    Geändert von OnlyFoo (07.12.08 um 16:04 Uhr)
     

Thema nicht erledigt

Ähnliche Themen

  1. [Quiz#15] OnlyFoo (C++)
    Von OnlyFoo im Forum Archiv
    Antworten: 1
    Letzter Beitrag: 11.04.10, 21:32
  2. [Quiz#9] OnlyFoo (C++)
    Von OnlyFoo im Forum Archiv
    Antworten: 0
    Letzter Beitrag: 30.07.09, 13:44
  3. [Quiz#9] OnlyFoo (C)
    Von OnlyFoo im Forum Archiv
    Antworten: 0
    Letzter Beitrag: 20.07.09, 19:15
  4. [QUIZ#2] OnlyFoo (C++)
    Von OnlyFoo im Forum Archiv
    Antworten: 0
    Letzter Beitrag: 30.09.08, 22:47
  5. [QUIZ#1] OnlyFoo (C)
    Von OnlyFoo im Forum Archiv
    Antworten: 0
    Letzter Beitrag: 19.09.08, 14:51