Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Code off

Name: Anonymous 2009-07-28 13:59

Welcome to the first annual /prog/ Code off !

The language will be C.

Compeptitors include (in no particular order):

Mr. Ribs
Xarn
FV
Anonymous
W. T. Snacks

Rumor has it, that there may be a special guest appearance from either The Sussman or
Leah Culver.

What's the point of this, you ask?

It's to determine who is an EXPERT PROGRAMMER.

Marking criteria:

Optimization
Elegance

Bonus marks:
indentation

Good luck!

Name: Xarn !Rmk.XarnE2!xGIX62dlJesBTK+ 2009-07-29 19:58

/* gcc file.c `allegro-config --libs` */

#include <stdlib.h>
#include <time.h>
#include <allegro.h>

#define WIDTH 1280
#define HEIGHT 800

#define SQUARE 10

#define FOREGROUND 255, 255, 255
#define BACKGROUND 0, 0, 0

typedef struct {
    int x;
    int y;
    int x_s;
    int y_s;
} square;

int main()
{
    int num_squares = 0, back, i;
    square *s = NULL;
    BITMAP *pad, *square;

    allegro_init();
    install_keyboard();
    install_mouse();
    install_timer();
   
    if (set_gfx_mode(GFX_AUTODETECT, WIDTH, HEIGHT, 0, 0) != 0)
        return 1;


    srand((unsigned int)time(NULL));

    pad = create_bitmap(WIDTH, HEIGHT);
    back = makecol(BACKGROUND);

    square = create_bitmap(SQUARE, SQUARE);
    clear_to_color(square, makecol(FOREGROUND));

    while (!keypressed()) {
        if (mouse_b & 1) {
            if (num_squares % 256 == 0)
                s = realloc(s, sizeof(square) * (num_squares + 256));

            s[num_squares].x = mouse_x;
            s[num_squares].y = mouse_y;
            s[num_squares].x_s = rand() % 64 - 50;
            s[num_squares].y_s = rand() % 64 - 50;

            ++num_squares;
        }

        clear_to_color(pad, back);

        for (i = 0; i < num_squares; ++i) {
            s[i].x += s[i].x_s;
            s[i].y += s[i].y_s;

            if (s[i].x <= 0) {
                s[i].x = 0;
                s[i].x_s *= -1;
            }
            if (s[i].x >= WIDTH - SQUARE) {
                s[i].x = WIDTH - SQUARE;
                s[i].x_s *= -1;
            }
            if (s[i].y <= 0) {
                s[i].y = 0;
                s[i].y_s *= -1;
            }
            if (s[i].y >= HEIGHT - SQUARE) {
                s[i].y = HEIGHT - SQUARE;
                s[i].y_s *= -1;
            }

            draw_sprite(pad, square, s[i].x, s[i].y);
        }

        show_mouse(pad);
        blit(pad, screen, 0, 0, 0, 0, WIDTH, HEIGHT);
        rest(30);

    }

    free(s);

    return 0;
   
} END_OF_MAIN()

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List