Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingPumpkin committed Mar 8, 2024
0 parents commit 9888653
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build:
gcc -Wall -lSDL2 -std=c99 ./src/*.c -o ./app

run:
./app

clean:
rm ./app
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SingleDigitDrawer
## Motivation
I'm working on project that will eventually become a digital clock widget-like app for personal use. While doing that I ran across a problem: turned out I had no nice-looking digits for the clock's face. That's why I made this utility that can draw a single digit imitating a seven segment display.

## Installation
The app uses SDL2 library so SDL2 is required to build and run it. Use my Makefile or write the gcc command manually.

## Usage
This utility is meant mostly for my personal use so it is pretty junky and I don't plan on adding a -\-help flag or anything. Either way if you want to play around with it this is what you should know:
1) You can set height and width of the window with -h and -w flags respectively (naturally you'll need to enter an integer after each flag)
2) After that the program will expect you to enter a digit that you want it to display. If you enter a number larger than 9 it will take its last digit and draw that.
3) To quit enter any negative number (no, it does not accept ctrl+C signal and it doesn't read any input from your mouse. <sub><sup> It's SDL, baby, those buttons aren't functional till I say so! B-) </sup></sub> )
177 changes: 177 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#include <stdio.h>
#include <SDL2/SDL.h>
#include <stdint.h>

SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
int init_window(int, int, int, int);
int destroy_window(void);
int running = 1;


SDL_Rect segments[7];
struct scaler
{
int smaller_side_size;
int larger_side_size;
int margin;
} sc;

const uint8_t states[10] =
{
0x77,//0
0x24,//1
0x5d,//2
0x6d,//3
0x2e,//4
0x6b,//5
0x7b,//6
0x25,//7
0x7f,//8
0x6f//9
};

void draw_digit(int);
void init_segments(void);
void calculate_scale(int, int);

int main(int argc, char* argv[])
{
int width = 400, height = 780;
int pos_x = SDL_WINDOWPOS_CENTERED, pos_y =SDL_WINDOWPOS_CENTERED;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--width") == 0)
{
if(sscanf(argv[++i], "%d", &width));
else width = 400;
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--height") == 0)
{
if(sscanf(argv[++i], "%d", &height));
else height = 800;
}
}
init_window(width, height, pos_x, pos_y);
calculate_scale(width, height);
init_segments();
int n = 0;
while (running)
{
draw_digit(n);
if (scanf("%d", &n))
{
if (n<0) running = 0;
}
else
{
printf("Invalid input!!!\n");
}
n = n%10;
}
destroy_window();
return 0;
}

void calculate_scale(int w, int h)
{
if (h > 2*w)
{
sc.smaller_side_size = w/10;
sc.margin = sc.smaller_side_size/5;
sc.larger_side_size = w - 2*sc.smaller_side_size - 2*sc.margin;
} else
{
sc.smaller_side_size = h/20;
sc.margin = sc.smaller_side_size/5;
sc.larger_side_size = (h - 3*sc.smaller_side_size - 4*sc.margin)/2;
}
}

void draw_digit(int n)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for (int i = 0; i<7; i++)
{
if((states[n]>>i) & 1)
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
else
SDL_SetRenderDrawColor(renderer, 64, 64, 64, 128);
SDL_RenderFillRect(renderer, &segments[i]);
}
SDL_RenderPresent(renderer);
}

void init_segments(void)
{
for (int i = 0; i<7; i++)
{
if(i%3 == 0)
{
segments[i].w = sc.larger_side_size;
segments[i].h = sc.smaller_side_size;
}
else
{
segments[i].h = sc.larger_side_size;
segments[i].w = sc.smaller_side_size;
}
}
segments[0].x = sc.smaller_side_size + sc.margin;
segments[0].y = 0;

segments[1].x = 0;
segments[1].y = sc.smaller_side_size + sc.margin;

segments[2].x = sc.larger_side_size + sc.smaller_side_size + 2*sc.margin;
segments[2].y = sc.smaller_side_size + sc.margin;

segments[3].x = sc.smaller_side_size + sc.margin;
segments[3].y = sc.larger_side_size + sc.smaller_side_size + 2*sc.margin;

segments[4].x = 0;
segments[4].y = sc.larger_side_size + 2*(sc.smaller_side_size + sc.margin);

segments[5].x = sc.larger_side_size + sc.smaller_side_size + 2*sc.margin;;
segments[5].y = sc.larger_side_size + 2*(sc.smaller_side_size + sc.margin);

segments[6].x = sc.smaller_side_size + sc.margin;
segments[6].y = 2*sc.larger_side_size + 2*sc.smaller_side_size + 4*sc.margin;
}

int init_window(int w, int h, int pos_x, int pos_y)
{
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
fprintf(stderr, "Error initializing SDL.\n");
return 1;
}
window = SDL_CreateWindow(
"SevenSegment",
pos_x,
pos_y,
w,
h,
SDL_WINDOW_SHOWN //| SDL_WINDOW_BORDERLESS
);
if (!window)
{
fprintf(stderr, "Error initializing window.\n");
return 1;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer)
{
fprintf(stderr, "Error initializing renderer.\n");
return 1;
}
return 0;
}

int destroy_window(void)
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

0 comments on commit 9888653

Please sign in to comment.