Quick actions

cmd+k|ctrl+k

Navigation

Languages

Ascii screen

Snippet info

Language

C

Visibility

public

Author

evine

Created

2016-03-19T02:53:00Z

Updated

2016-03-19T20:48:35Z

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

// Ignore instead??
#define Pixel(Name, x, y) Name.Screen[ \
(assert((x) < Name.Width), assert((x) >= 0), (x)) + \
(assert((y) < Name.Height), assert((y) >= 0), (y)) * (Name.Width+1)+1 ]


typedef struct{
    int Width;
    int Height;
    int Size;
    char *Screen;
}screen;

screen ScreenInit(int Width, int Height)
{
    screen Result = {Width, Height};
    Result.Size = (Result.Width + 1) * Result.Height + 1;
    Result.Screen = malloc(Result.Size);
    
    for(int i = 0; i < Result.Size; ++i)
        Result.Screen[i] = ' ';
    for(int i = 0; i < Result.Height; ++i)
    {
        Result.Screen[(Result.Width+1) * i] = '\n';
    }
    Result.Screen[Result.Size-1] = 0;
    
    return Result;
}

void Print(screen Screen, int x, int y, char *String);
void Square(screen Screen, int x, int y, int w, int h);

int main()
{
    screen Screen = ScreenInit(80, 40);
    #define p(x, y) Pixel(Screen, x, y)
    
    Square(Screen,20,2,9,4);
    Print(Screen,20,2,"Apples");
    Print(Screen,20,3,"and Pears");
    Print(Screen,20,4,"does not");
    Print(Screen,20,5,"approve.");
    
    p(0,0) = 'A';
    p(2,4) = p(0,0);
    p(3,4) = 'P';
    p(4,4) = 'P';
    p(5,4) = 'L';
    p(6,4) = 'E';
    p(7,4) = 'J';
    p(10,4) = 'K';
    
    
    
    for(int i = 0; i < 40; ++i)
    {
        p(i*2, i) = '0'+(i/10)%10;
        p(i*2+1, i) = '0'+i%10;
    }
    printf(Screen.Screen);
    return 0;
}

void Print(screen Screen, int x, int y, char *String)
{
    for(int i = 0; String[i] != 0; ++i)
    {
        p(x+i,y) = String[i];
    }
}

void Square(screen Screen, int x, int y, int w, int h)
{
    for(int i = 0; i < w; ++i)
    {
        p(i+x,y-1) = '-';
        p(i+x,y+h) = '-';
    }
    for(int i = 0; i < h; ++i)
    {
        p(x-1,y+i) = '|';
        p(x+w,y+i) = '|';
    }
    p(x-1,y-1) = '+';
    p(x+w,y-1) = '+';
    p(x-1,y+h) = '+';
    p(x+w,y+h) = '+';
}
INFO