summaryrefslogtreecommitdiff
path: root/frontend/sdl
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/sdl')
-rw-r--r--frontend/sdl/.border.jpeg-autosave.krabin0 -> 71027 bytes
-rw-r--r--frontend/sdl/sdl_ui.c99
2 files changed, 92 insertions, 7 deletions
diff --git a/frontend/sdl/.border.jpeg-autosave.kra b/frontend/sdl/.border.jpeg-autosave.kra
new file mode 100644
index 0000000..69f596a
--- /dev/null
+++ b/frontend/sdl/.border.jpeg-autosave.kra
Binary files differ
diff --git a/frontend/sdl/sdl_ui.c b/frontend/sdl/sdl_ui.c
index 012255a..f862244 100644
--- a/frontend/sdl/sdl_ui.c
+++ b/frontend/sdl/sdl_ui.c
@@ -1,15 +1,19 @@
#include <SDL2/SDL.h>
+#include <SDL2/SDL_ttf.h>
#include "cetris.h"
-#define W 360
-#define H 640
+#define W 430
+#define H 550
+#define BOARD_OFFSET_X 25
+#define BOARD_OFFSET_Y 20
struct cetris_game g;
SDL_Renderer* render;
SDL_Texture* block;
SDL_Event e;
+TTF_Font* font;
typedef struct {
int r;
@@ -31,22 +35,103 @@ sdl_color colors[8] = {
void setup() {
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
SDL_Window* win = SDL_CreateWindow("Cetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN);
- render = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC);
+ render = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED);
+ SDL_RenderSetLogicalSize(render, W, H);
if (!render) exit(fprintf(stderr, "[Error] Could not create SDL renderer\n"));
SDL_Surface* s = SDL_LoadBMP("block.bmp");
block = SDL_CreateTextureFromSurface(render, s);
SDL_FreeSurface(s);
+
+ TTF_Init();
+ font = TTF_OpenFont("LiberationMono-Regular.ttf", 18);
+}
+
+void draw_text(char* string, int x, int y) {
+ int w, h;
+ char msg[80];
+ snprintf(msg, 80, string);
+ TTF_SizeText(font, msg, &w, &h);
+ SDL_Surface *msgsurf = TTF_RenderText_Blended(font, msg, (SDL_Color){255, 255, 255, 255});
+ SDL_Texture *msgtex = SDL_CreateTextureFromSurface(render, msgsurf);
+ SDL_Rect fromrec = {0, 0, msgsurf->w, msgsurf->h};
+ SDL_Rect torec = {x, y, msgsurf->w, msgsurf->h};
+ SDL_RenderCopy(render, msgtex, &fromrec, &torec);
+ SDL_DestroyTexture(msgtex);
+ SDL_FreeSurface(msgsurf);
}
void draw_stuff() {
+ SDL_SetRenderDrawColor(render, 100, 100, 112, 255);
SDL_RenderClear(render);
+ SDL_Rect b = {BOARD_OFFSET_X, BOARD_OFFSET_Y, 250, 500};
+ SDL_SetRenderDrawColor(render, 120, 120, 132, 255);
+ SDL_RenderFillRect(render, &b);
+ SDL_RenderDrawRect(render, &b);
+
+
+ SDL_Rect next = {294, 20, 125, 125};
+ SDL_RenderFillRect(render, &next);
+ SDL_RenderDrawRect(render, &next);
+ int index = g.current_index;
+ sdl_color nc = colors[g.piece_queue[index].c];
+ SDL_SetTextureColorMod(block, nc.r, nc.g, nc.b);
+ SDL_Rect p = {294, 20, 25, 25};
+ for (int x = 0; x < 4; x++) {
+ for (int y = 0; y < 4; y++) {
+ if (g.piece_queue[index].mat[y][x]) {
+ p.x = 319 + (x * 25);
+ p.y = 30 + (y * 25);
+ if (g.piece_queue[index].t == I) {
+ p.x = 306 + (x * 25);
+ }
+ if (g.piece_queue[index].t == O) {
+ p.x = 306 + (x * 25);
+ }
+ SDL_RenderCopy(render, block, NULL, &p);
+ }
+ }
+ }
+
+ SDL_SetRenderDrawColor(render, 76, 70, 72, 255);
+
+ int y_off = 140;
+
+ SDL_Rect s = {290, 20 + y_off, 135, 42};
+ SDL_RenderFillRect(render, &s);
+ SDL_RenderDrawRect(render, &s);
+ draw_text("SCORE", 295, 23 + y_off);
+ char* score = malloc(sizeof(char) * 25);
+ sprintf(score, "%li", g.score);
+ draw_text(score, 294 + strlen(score), 40 + y_off);
+ free(score);
+
+ SDL_Rect l = {290, 70 + y_off, 135, 42};
+ SDL_RenderFillRect(render, &l);
+ SDL_RenderDrawRect(render, &l);
+ draw_text("LEVEL", 295, 73 + y_off);
+ char* level = malloc(sizeof(char) * 25);
+ sprintf(level, "%i", g.level);
+ draw_text(level, 294 + strlen(level), 90 + y_off);
+ free(level);
+
+ SDL_Rect l1 = {290, 120 + y_off, 135, 42};
+ SDL_RenderFillRect(render, &l1);
+ SDL_RenderDrawRect(render, &l1);
+ draw_text("LINES", 295, 123 + y_off);
+ char* lines = malloc(sizeof(char) * 25);
+ sprintf(lines, "%i", g.lines);
+ draw_text(lines, 294 + strlen(lines), 140 + y_off);
+ free(lines);
+
SDL_SetRenderDrawColor(render, 255, 255, 255, 124);
for (int x = 0; x < BOARD_X + 1; x++) {
- SDL_RenderDrawLine(render, 1 + (x * 25), 1, 1 + (x * 25), 500);
+ int rx = BOARD_OFFSET_X + 1 + (x * 25);
+ SDL_RenderDrawLine(render, rx, BOARD_OFFSET_Y + 1, rx, BOARD_OFFSET_Y + 500);
}
for (int y = 0; y < BOARD_Y - BOARD_VISABLE + 1; y++) {
- SDL_RenderDrawLine(render, 1, 1 + (y * 25), 250, 1 + (y * 25));
+ int ry = BOARD_OFFSET_Y + (y * 25);
+ SDL_RenderDrawLine(render, BOARD_OFFSET_X + 1, ry, BOARD_OFFSET_X + 250, ry);
}
SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
@@ -57,8 +142,8 @@ void draw_stuff() {
if (g.board[x][y].occupied) {
c = colors[g.board[x][y].c];
SDL_SetTextureColorMod(block, c.r, c.g, c.b);
- r.x = x * 25;
- r.y = (y - BOARD_VISABLE) * 25;
+ r.x = BOARD_OFFSET_X + x * 25;
+ r.y = BOARD_OFFSET_Y + (y - BOARD_VISABLE) * 25;
SDL_RenderCopy(render, block, NULL, &r);
}
}