Added a counter system

Each time the square is touched, the counter increments. It uses
SDL_ttf to render the counter. As simple as this addition may be,
I had to surprisingly add a buttload of code to render a damn
number. Low level programming is such a lovely experience :').
This commit is contained in:
Ty3r0X 2023-12-30 20:53:54 +02:00
parent 9281a0d505
commit 14f82abb25
No known key found for this signature in database
GPG Key ID: 1987C830BBC99F38
5 changed files with 85 additions and 18 deletions

BIN
arimo.ttf Normal file

Binary file not shown.

View File

@ -23,6 +23,7 @@ LIB :=\
c\
SDL2main\
SDL2\
SDL2_ttf
INCDIR :=\
src/

View File

@ -28,6 +28,7 @@
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_video.h>
#include <stdio.h>
#include <stdlib.h>
@ -39,14 +40,21 @@ SDL_Renderer *main_render;
SDL_Event *main_event;
SDL_Texture *background;
SDL_Rect *rectangle;
SDL_Surface *text_surface;
SDL_Texture *text_texture;
TTF_Font *text_font;
SDL_Color *text_color;
SDL_Rect counter_box;
int
init_program (void) {
/* Initialization Checks */
if (SDL_Init (SDL_INIT_VIDEO) != 0) {
if (SDL_Init (SDL_INIT_VIDEO) < 0)
return fprintf (stderr, "SDL initialization error: %s\n", SDL_GetError ());
}
if (TTF_Init () < 0)
return fprintf (stderr, "Font initialization error: %s\n", SDL_GetError ());
/* window variable initialization */
@ -64,7 +72,11 @@ init_program (void) {
/* Background initialization */
main_render = SDL_CreateRenderer (window, -1, 0);
background = SDL_CreateTexture (main_render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
background = SDL_CreateTexture (main_render,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
SCREEN_WIDTH,
SCREEN_HEIGHT);
/* Initialize rectangle*/
@ -72,6 +84,18 @@ init_program (void) {
rectangle->w = RECT_SIZE;
rectangle->h = RECT_SIZE;
/* Counter initialization */
counter_box.x = COUNTER_BOX_X;
counter_box.y = COUNTER_BOX_Y;
text_font = TTF_OpenFont ("arimo.ttf", COUNTER_TEXT_SIZE);
text_surface = NULL;
text_texture = NULL;
text_color = calloc (1, sizeof (SDL_Color));
text_color->r = 255;
text_color->g = 255;
text_color->b = 255;
/* A new variable is created called main_event, which is a SDL_Event type, which itself is a structure
* of multiple structures... of events... */

View File

@ -29,15 +29,14 @@
#include <stdbool.h>
#include <stdlib.h>
#define HEX_POKE 0xFF
#define HEX_POKE 0xFF
#define COUNTER_DIGITS 100
int
main (int argc, char *argv[]) {
struct coords {
int x;
int y;
} ray;
struct pos ray;
int counter = 0;
Uint8 bg_red = 0x00;
Uint8 bg_green = HEX_POKE;
@ -57,20 +56,47 @@ main (int argc, char *argv[]) {
for (;;) {
/* Initialize counter array - max 100 digits */
char counter_text[COUNTER_DIGITS];
/* Constantly initialize boolean values for corners, each for loop
* iteration they change values */
bool x_limit = (rectangle->x <= 0 || rectangle->x >= SCREEN_WIDTH - RECT_SIZE);
bool y_limit = (rectangle->y <= 0 || rectangle->y >= SCREEN_HEIGHT - RECT_SIZE);
/* Insert counter values into the array */
snprintf (counter_text, sizeof (counter_text), "%d", counter);
text_surface = TTF_RenderText_Solid (text_font, counter_text, *text_color);
text_texture = SDL_CreateTextureFromSurface (main_render, text_surface);
/* Snatch counter_box's width and size by querying text_surface */
SDL_QueryTexture (text_texture, NULL, NULL, &counter_box.w, &counter_box.h);
/* Background render*/
SDL_SetRenderTarget (main_render, background);
SDL_SetRenderDrawColor (main_render, bg_red, bg_green, bg_blue, 0x00);
SDL_RenderClear (main_render);
/* Rectangle render */
SDL_RenderDrawRect (main_render, rectangle);
SDL_SetRenderDrawColor (main_render, HEX_POKE, 0x00, 0x00, 0x00);
SDL_RenderFillRect (main_render, rectangle);
SDL_SetRenderTarget (main_render, NULL);
SDL_RenderCopy (main_render, background, NULL, NULL);
/* Counter render */
SDL_RenderCopy (main_render, text_texture, NULL, &counter_box);
/* All finished, time to show on screen */
SDL_RenderPresent (main_render);
/* Normally the rect constantly goes up diagonally, if it reaches a
@ -98,7 +124,9 @@ main (int argc, char *argv[]) {
case SDL_MOUSEMOTION:
if (check_interaction_in_rect (main_event->motion.x, main_event->motion.y, rectangle)) {
printf ("Mouse cursor is inside the square at position (%d,%d)\n", main_event->motion.x, main_event->motion.y);
printf ("Mouse cursor is inside the square at position (%d,%d)\n",
main_event->motion.x,
main_event->motion.y);
ray.x *= -1;
ray.y *= -1;
rectangle->x = rand () % (int) (SCREEN_WIDTH - 1 - RECT_SIZE);
@ -106,18 +134,20 @@ main (int argc, char *argv[]) {
bg_red = rand () & HEX_POKE;
bg_green = rand () & HEX_POKE;
bg_blue = rand () & HEX_POKE;
counter++;
}
break;
default:
break;
/* Constantly increment / decrement rectangle position */
rectangle->x = rectangle->x + (1 * ray.x);
rectangle->y = rectangle->y + (1 * ray.y);
SDL_Delay (3);
}
/* Constantly increment / decrement rectangle position */
rectangle->x = rectangle->x + (1 * ray.x);
rectangle->y = rectangle->y + (1 * ray.y);
/* Add a delay, let the CPU breathe */
SDL_Delay (3);
}
}

View File

@ -23,9 +23,11 @@
#define WINDOW_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_ttf.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@ -41,14 +43,24 @@
#define RECT_SIZE 100
#define COUNTER_BOX_X 10
#define COUNTER_BOX_Y 530
#define COUNTER_TEXT_SIZE 48
extern SDL_Window *window;
extern SDL_Renderer *main_render;
extern SDL_Texture *background;
extern SDL_Rect *rectangle;
extern SDL_Event *main_event;
extern SDL_Surface *text_surface;
extern SDL_Texture *text_texture;
extern TTF_Font *text_font;
extern SDL_Color *text_color;
extern SDL_Rect counter_box;
struct pos {
Uint16 x;
Uint16 y;
int x;
int y;
};
int init_program (void);