Fridge: code refactoring

git-svn-id: svn://kolibrios.org@8378 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
maxcodehack 2020-12-12 16:49:35 +00:00
parent 21a4f19495
commit e6e99fe316
2 changed files with 56 additions and 56 deletions

View File

@ -9,5 +9,8 @@ INCLUDES = INCLUDES .. " -I../../../contrib/C_Layer/INCLUDE"
table.insert(LIBDEPS, "../../../contrib/C_Layer/OBJ/<C_Layer>") table.insert(LIBDEPS, "../../../contrib/C_Layer/OBJ/<C_Layer>")
LIBS = LIBS .. " ../../../contrib/C_Layer/OBJ/loadlibimg.o" LIBS = LIBS .. " ../../../contrib/C_Layer/OBJ/loadlibimg.o"
-- Subsystem native
LDFLAGS = LDFLAGS .. " --subsystem native"
compile_gcc{"fridge.c"} compile_gcc{"fridge.c"}
link_gcc("fridge") link_gcc("fridge")

View File

@ -1,13 +1,15 @@
// Includes //
#include <kos32sys.h> #include <kos32sys.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <kolibri_libimg.h> #include <kolibri_libimg.h>
/* // #include "mp3.h"
#include "mp3.h"
*/
//EVENTS LOOK LIKE IN C-- // C-- event defines //
#define evReDraw 1 #define evReDraw 1
#define evKey 2 #define evKey 2
#define evButton 3 #define evButton 3
@ -19,29 +21,28 @@
#define evDebug 9 #define evDebug 9
#define RESTART 99 // Code //
#define button_color 0xbbbbbb
#define button_size 44
#define b_color 0xbbbbbb #define field_size 4
#define _size 4
#define bs 44
int field[_size][_size] = { int field[field_size][field_size] = {
{0, 0, 0, 1}, {0, 0, 0, 1},
{0, 0, 1, 0}, {0, 0, 1, 0},
{0, 0, 0, 1}, {0, 0, 0, 1},
{1, 0, 0, 0} {1, 0, 0, 0}
}; };
short vict = 0;
short debug_pr = 0;
char* title = "Fridge"; char* title = "Fridge";
#define BUTTON_RESTART 99
short victory = 0;
// PICTURES LOADING CODE // Load pictures //
char temp_path[4096]; char temp_path[4096];
char* HOR; char* HORIZONTAL_IMAGE;
char* VER; char* VERTICAL_IMAGE;
char* load_file_inmem(char* fname, int32_t* read_sz) char* load_file_inmem(char* fname, int32_t* read_sz)
{ {
@ -52,13 +53,13 @@ char* load_file_inmem(char* fname, int32_t* read_sz)
if (fseek(f, 0, SEEK_END)) { if (fseek(f, 0, SEEK_END)) {
exit(1); exit(1);
} }
int filesize = ftell(f); int filefield_size = ftell(f);
rewind(f); rewind(f);
char* fdata = malloc(filesize); char* fdata = malloc(filefield_size);
if(!fdata) { if(!fdata) {
exit(1); exit(1);
} }
*read_sz = fread(fdata, 1, filesize, f); *read_sz = fread(fdata, 1, filefield_size, f);
if (ferror(f)) { if (ferror(f)) {
exit(1); exit(1);
} }
@ -67,9 +68,8 @@ char* load_file_inmem(char* fname, int32_t* read_sz)
return fdata; return fdata;
} }
void load_pictures() {
void load_pict() { const int icon_rgb_field_size = button_size*button_size;
const int icon_rgb_size = bs*bs;
char *image_data, char *image_data,
*filedata; *filedata;
@ -77,49 +77,50 @@ void load_pict() {
int32_t read_bytes; int32_t read_bytes;
filedata = load_file_inmem(temp_path, &read_bytes); filedata = load_file_inmem(temp_path, &read_bytes);
HOR = malloc(icon_rgb_size * 3); HORIZONTAL_IMAGE = malloc(icon_rgb_field_size * 3);
image_data = img_decode(filedata, read_bytes, 0); image_data = img_decode(filedata, read_bytes, 0);
img_to_rgb2(image_data, HOR); img_to_rgb2(image_data, HORIZONTAL_IMAGE);
strcpy(temp_path, "v.png"); strcpy(temp_path, "v.png");
filedata = load_file_inmem(temp_path, &read_bytes); filedata = load_file_inmem(temp_path, &read_bytes);
VER = malloc(icon_rgb_size * 3); VERTICAL_IMAGE = malloc(icon_rgb_field_size * 3);
image_data = img_decode(filedata, read_bytes, 0); image_data = img_decode(filedata, read_bytes, 0);
img_to_rgb2(image_data, VER); img_to_rgb2(image_data, VERTICAL_IMAGE);
img_destroy(image_data); img_destroy(image_data);
free(filedata); free(filedata);
} }
// END OF PICTURES LOAD CODE
// GUI functions //
void redraw_buttons() { void redraw_buttons() {
for (int j = 5, yy = 0; yy<_size; j+=bs, yy++) for (int j = 5, x = 0; x<field_size; j+=button_size, x++)
for (int i = 15, xx = 0; xx<_size; i+=bs, xx++) for (int i = 15, y = 0; y<field_size; i+=button_size, y++)
{ {
define_button(65536 * i + (bs-1), 65536 * j + (bs-1), ((xx+1)*10)+yy+1, b_color); // 0x50 mean button without drawing, but with border when press
// ((y+1)*10)+x+1 mean button id
define_button(65536 * i + (button_size), 65536 * j + (button_size), (0x50 << 24) | ((y+1)*10)+x+1, 0);
if (field[yy][xx]) draw_bitmap(VER, i, j, bs, bs); if (field[x][y]) draw_bitmap(VERTICAL_IMAGE, i, j, button_size, button_size);
else draw_bitmap(HOR, i, j, bs, bs); else draw_bitmap(HORIZONTAL_IMAGE, i, j, button_size, button_size);
} }
} }
void draw_game_window(){ void draw_game_window(){
BeginDraw(); BeginDraw();
DrawWindow(215,100,220, 220,title,b_color,0x34); DrawWindow(215, 100, 220, 220, title, button_color, 0x34);
redraw_buttons(); redraw_buttons();
EndDraw(); EndDraw();
} }
// Need refactoring:
static inline static inline
void draw_text_sysNEW(const char *text, int x, int y, int len, int fontType, color_t color) void draw_text_sysNEW(const char *text, int x, int y, int len, int fontType, color_t color)
{ {
@ -132,27 +133,23 @@ void draw_text_sysNEW(const char *text, int x, int y, int len, int fontType, col
} }
void SetUp() { void SetUp() {
for (int y = 0; y<_size; y++) for (int y = 0; y<field_size; y++)
for (int x = 0; x<_size; x++) for (int x = 0; x<field_size; x++)
{ {
field[x][y] = rand() % 2; field[x][y] = rand() % 2;
} }
} }
// Need refactoring:
void draw_victory_window() { void draw_victory_window() {
BeginDraw(); BeginDraw();
DrawWindow(215,100,220, 220,title,b_color,0x34); DrawWindow(215,100,220, 220,title,button_color,0x34);
draw_text_sysNEW("Ну вы, и", 10, 10, strlen("Ну вы, и"), 0xB1, 0x000000); draw_text_sysNEW("Ну вы, и", 10, 10, strlen("Ну вы, и"), 0xB1, 0x000000);
draw_text_sysNEW("медвежатник,", 10, 50, strlen("Ну вы, и медвежатник,"), 0xB1, 0x000000); draw_text_sysNEW("медвежатник,", 10, 50, strlen("Ну вы, и медвежатник,"), 0xB1, 0x000000);
draw_text_sysNEW("Шеф!", 12, 90, strlen("Шеф!"), 0xB1, 0x000000); draw_text_sysNEW("Шеф!", 12, 90, strlen("Шеф!"), 0xB1, 0x000000);
if (debug_pr) { define_button(65536 * ((220/2)-(50)) + 140, 65536 * 140 + 25+12, BUTTON_RESTART, 0x9A9A9A);
printf("Fridge: Very great!\n");
debug_pr = 0;
}
define_button(65536 * ((220/2)-(50)) + 140, 65536 * 140 + 25+12, RESTART, 0x9A9A9A);
draw_text_sysNEW("Заново", 80, 145, strlen("Заново"), 0xB1, 0x000000); draw_text_sysNEW("Заново", 80, 145, strlen("Заново"), 0xB1, 0x000000);
EndDraw(); EndDraw();
} }
@ -162,7 +159,7 @@ void draw_victory_window() {
void Button() { void Button() {
int id = get_os_button(); int id = get_os_button();
if (id == 1) exit(0); else if (id == 1) exit(0); else
if (id == RESTART) { if (id == BUTTON_RESTART) {
SetUp(); SetUp();
vict = 0; vict = 0;
draw_game_window(); draw_game_window();
@ -173,10 +170,10 @@ void Button() {
int x = (id/10)-1; int x = (id/10)-1;
int y = (id%10)-1; int y = (id%10)-1;
for (int i = 0; i<_size; i++) for (int i = 0; i<field_size; i++)
if (field[i][x]) field[i][x] = 0; else field[i][x] = 1; if (field[i][x]) field[i][x] = 0; else field[i][x] = 1;
for (int i = 0; i<_size; i++) for (int i = 0; i<field_size; i++)
if (field[y][i]) field[y][i] = 0; else field[y][i] = 1; if (field[y][i]) field[y][i] = 0; else field[y][i] = 1;
if (field[y][x]) field[y][x] = 0; else field[y][x] = 1; if (field[y][x]) field[y][x] = 0; else field[y][x] = 1;
@ -188,8 +185,8 @@ void Button() {
int fridge_opened() { int fridge_opened() {
int fr_op = 0; int fr_op = 0;
for (int y = 0; y<_size; y++) for (int y = 0; y<field_size; y++)
for (int x = 0; x<_size; x++) for (int x = 0; x<field_size; x++)
{ {
fr_op += field[x][y]; fr_op += field[x][y];
} }
@ -207,7 +204,8 @@ int main()
printf("Can not load libimg.obj!\n"); printf("Can not load libimg.obj!\n");
exit(1); exit(1);
} }
load_pict();
load_pictures();
draw_game_window(); draw_game_window();
while(1) while(1)
@ -217,8 +215,7 @@ int main()
case evButton: case evButton:
Button(); Button();
if (fridge_opened()) { if (fridge_opened()) {
vict = 1; victory = 1;
debug_pr = 1;
draw_victory_window(); draw_victory_window();
} }
break; break;
@ -228,7 +225,7 @@ int main()
break; break;
case evReDraw: case evReDraw:
if (!vict) draw_game_window(); if (!victory) draw_game_window();
else draw_victory_window(); else draw_victory_window();
break; break;
} }