Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions elkscmd/Applications
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ gui/images/cls.bmp ::lib/cls.bmp :gui :1440k
gui/images/save.bmp ::lib/save.bmp :gui :1440k
gui/images/quit.bmp ::lib/quit.bmp :gui :1440k
gui/images/paint.bmp ::lib/paint.bmp :gui :1440k
gui/images/fill.bmp ::lib/fill.bmp :gui :1440k
gui/images/brush.bmp ::lib/brush.bmp :gui :1440k
busyelks/busyelks :busyelks
inet/httpd/sample_index.html ::var/www/index.html :net
ktcp/ktcp :net
Expand Down
211 changes: 79 additions & 132 deletions elkscmd/gui/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ app_t DrawingApp;

boolean_t drawing; // Are we drawing with the left mouse button?
boolean_t altdrawing; // Are we drawing with the right mouse button?
boolean_t floodFill; // Are we flood filling with the left mouse button?
boolean_t floodFillCalled; // True if user is trying to floodfill
int mx,my; // Mouse X and Y
int omx, omy; // Old MouseX and Old MouseY (pos at previous update)
Expand All @@ -38,10 +39,7 @@ void A_InitTomentPainter(void)
graphics_open(EGA? VGA_640x350x16: VGA_640x480x16);

DrawingApp.quit = false;

// for(int y = 0; y < SCREEN_HEIGHT; y++)
// for(int x = 0; x < SCREEN_WIDTH; x++)
// drawpixel(x, y, BLACK);
floodFill = false;

A_InitPalette();
}
Expand All @@ -61,132 +59,81 @@ void A_InitPalette(void)


// Create and configure buttons
#if UNUSED
paletteButtons[0].name = "BrightnessSelector";
paletteButtons[0].box.x = 775;
paletteButtons[0].box.y = 10;
paletteButtons[0].box.w = 16;
paletteButtons[0].box.h = 64;
paletteButtons[0].OnClick = G_BrightnessButtonOnClick;
paletteButtons[0].render = false;
#endif
paletteButtons[1].name = "ColorPicker";
paletteButtons[1].box.x = SCREEN_WIDTH + 10;
paletteButtons[1].box.y = 10;
paletteButtons[1].box.w = 128;
paletteButtons[1].box.h = 128;
paletteButtons[1].OnClick = G_ColorPickerOnClick;
paletteButtons[1].render = false;

// Bush Sizes 1
paletteButtons[2].name = "BushSize1";
paletteButtons[2].box.x = SCREEN_WIDTH + 11 + 32*0;
paletteButtons[2].box.y = 195;
paletteButtons[2].box.w = 30;
paletteButtons[2].box.h = 30;
paletteButtons[2].OnClick = G_SetBushSize;
paletteButtons[2].data1 = 1;
paletteButtons[2].render = true;

// Bush Sizes 2
paletteButtons[3].name = "BushSize2";
paletteButtons[3].box.x = SCREEN_WIDTH + 11 + 32*1;
paletteButtons[3].box.y = 195;
paletteButtons[3].box.w = 30;
paletteButtons[3].box.h = 30;
paletteButtons[3].OnClick = G_SetBushSize;
paletteButtons[3].data1 = 2;
paletteButtons[3].render = true;

// Bush Sizes 3
paletteButtons[4].name = "BushSize3";
paletteButtons[4].box.x = SCREEN_WIDTH + 11 + 32*2;
paletteButtons[4].box.y = 195;
paletteButtons[4].box.w = 30;
paletteButtons[4].box.h = 30;
paletteButtons[4].OnClick = G_SetBushSize;
paletteButtons[4].data1 = 3;
paletteButtons[4].render = true;

// Bush Sizes 4
paletteButtons[5].name = "BushSize4";
paletteButtons[5].box.x = SCREEN_WIDTH + 11 + 32*3;
paletteButtons[5].box.y = 195;
paletteButtons[5].box.w = 30;
paletteButtons[5].box.h = 30;
paletteButtons[5].OnClick = G_SetBushSize;
paletteButtons[5].data1 = 4;
paletteButtons[5].render = true;

// Bush Sizes 5
paletteButtons[6].name = "BushSize5";
paletteButtons[6].box.x = SCREEN_WIDTH + 11 + 32*0;
paletteButtons[6].box.y = 227;
paletteButtons[6].box.w = 30;
paletteButtons[6].box.h = 30;
paletteButtons[6].OnClick = G_SetBushSize;
paletteButtons[6].data1 = 6;
paletteButtons[6].render = true;

// Bush Sizes 6
paletteButtons[7].name = "BushSize6";
paletteButtons[7].box.x = SCREEN_WIDTH + 11 + 32*1;
paletteButtons[7].box.y = 227;
paletteButtons[7].box.w = 30;
paletteButtons[7].box.h = 30;
paletteButtons[7].OnClick = G_SetBushSize;
paletteButtons[7].data1 = 8;
paletteButtons[7].render = true;

// Bush Sizes 7
paletteButtons[8].name = "BushSize7";
paletteButtons[8].box.x = SCREEN_WIDTH + 11 + 32*2;
paletteButtons[8].box.y = 227;
paletteButtons[8].box.w = 30;
paletteButtons[8].box.h = 30;
paletteButtons[8].OnClick = G_SetBushSize;
paletteButtons[8].data1 = 10;
paletteButtons[8].render = true;

// Bush Sizes 8
paletteButtons[9].name = "BushSize8";
paletteButtons[9].box.x = SCREEN_WIDTH + 11 + 32*3;
paletteButtons[9].box.y = 227;
paletteButtons[9].box.w = 30;
paletteButtons[9].box.h = 30;
paletteButtons[9].OnClick = G_SetBushSize;
paletteButtons[9].data1 = 12;
paletteButtons[9].render = true;

// Save
paletteButtons[10].name = "SaveButton";
paletteButtons[10].box.x = SCREEN_WIDTH + 10;
paletteButtons[10].box.y = 300;
paletteButtons[10].box.w = 32;
paletteButtons[10].box.h = 32;
paletteButtons[10].OnClick = G_SaveButtonOnClick;
paletteButtons[10].render = true;
paletteButtons[10].fileName = LIBPATH "save.bmp";

// Clear Screen
paletteButtons[11].name = "Cls";
paletteButtons[11].box.x = SCREEN_WIDTH + 10 + 32*2;
paletteButtons[11].box.y = 300;
paletteButtons[11].box.w = 32;
paletteButtons[11].box.h = 32;
paletteButtons[11].OnClick = G_ClearScreen;
paletteButtons[11].render = true;
paletteButtons[11].fileName = LIBPATH "cls.bmp";

// Quit
paletteButtons[12].name = "QuitButton";
paletteButtons[12].box.x = SCREEN_WIDTH + 10 + 32*3;
paletteButtons[12].box.y = 300;
paletteButtons[12].box.w = 32;
paletteButtons[12].box.h = 32;
paletteButtons[12].OnClick = G_QuitButtonOnClick;
paletteButtons[12].render = true;
paletteButtons[12].fileName = LIBPATH "quit.bmp";
// --- Helper macro to initialize buttons ---
#define INIT_BUTTON(ID, LABEL, X, Y, W, H, CLICK, RENDER) \
paletteButtons[ID].name = LABEL; \
paletteButtons[ID].box.x = (X); \
paletteButtons[ID].box.y = (Y); \
paletteButtons[ID].box.w = (W); \
paletteButtons[ID].box.h = (H); \
paletteButtons[ID].OnClick = CLICK; \
paletteButtons[ID].render = RENDER;

#if UNUSED
INIT_BUTTON(0, "BrightnessSelector", 775, 10, 16, 64, G_BrightnessButtonOnClick, 0);
#endif

// --- Color Picker ---
INIT_BUTTON(1, "ColorPicker", CANVAS_WIDTH + 10, 10, 128, 128, G_ColorPickerOnClick, 0);

// --- Brush Size Buttons (8 total in two rows) ---
{
int brushSizes[8] = {1, 2, 3, 4,
6, 8, 10, 12};
char* brushNames[8] = {
"BushSize1", "BushSize2", "BushSize3", "BushSize4",
"BushSize5", "BushSize6", "BushSize7", "BushSize8"
};

for (int i = 0; i < 8; ++i) {
int index = i + 2;
int col = i % 4;
int row = i / 4;
INIT_BUTTON(index,
brushNames[i],
CANVAS_WIDTH + 11 + 32 * col,
195 + row * 32,
30, 30,
G_SetBushSize, 1);
paletteButtons[index].data1 = brushSizes[i];
}
}

// --- Utility Buttons ---
{
int utilCount = 5;
int indices[] = {10, 11, 12, 13, 14};
char* names[] = {"SaveButton", "FillButton", "BrushButton", "QuitButton", "Cls"};
int offsetX[] = {0, 32, 0, 32 * 3, 32 * 3};
int offsetY[] = {0, -32, -32, 0, -32};
Copy link
Copy Markdown
Owner

@ghaerr ghaerr Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you aware how expensive this is, to declare auto initialized array variables? The initializations have to be saved in the data segment at compile time, but then at runtime copied via an internal call to memcpy to get them on the stack; IMO lots of extra code for nothing.

I recommend adding 'static' to the declarations which achieves the same result with lots less code. Same for the variables on lines 81-85.

(You can use ia16-elf-objdump -D -r -Mi8086 app.oaj to see code generated).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, no I was not aware of this. I was just looking for a way to simplify new buttons addition but I couldn’t achieve it with macros only.
I will rewrite it on next PR

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new coding and ease of inspection of the initializers for each case, I don't think any rewrite is necessary except just applying static prefix to the declarations of the arrays (only).

int (*handlers[])(struct button_s*) = {
G_SaveButtonOnClick,
G_SetFloodFill,
G_SetBrush,
G_QuitButtonOnClick,
G_ClearScreen
};
char* files[] = {
LIBPATH "save.bmp",
LIBPATH "fill.bmp",
LIBPATH "brush.bmp",
LIBPATH "quit.bmp",
LIBPATH "cls.bmp"
};

for (int i = 0; i < utilCount; ++i) {
INIT_BUTTON(indices[i],
names[i],
CANVAS_WIDTH + 10 + offsetX[i],
300 + offsetY[i],
32, 32,
handlers[i],
1);
paletteButtons[indices[i]].fileName = files[i];
}
}

#undef INIT_BUTTON
}


Expand All @@ -197,10 +144,10 @@ void A_GameLoop(void)
{
movecursor(mx, my);

if((drawing || altdrawing) && mx > SCREEN_WIDTH) mx = SCREEN_WIDTH;
if((drawing || altdrawing) && mx > CANVAS_WIDTH) mx = CANVAS_WIDTH;

// In canvas
if(mx <= SCREEN_WIDTH)
if(mx <= CANVAS_WIDTH)
{
mouseOnPalette = false;

Expand Down
9 changes: 5 additions & 4 deletions elkscmd/gui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define APPLICATION_H_INCLUDED

// Drawing + Palette
#define SCREEN_WIDTH (SCREENWIDTH-PALETTE_WIDTH)
#define SCREEN_HEIGHT SCREENHEIGHT
#define CANVAS_WIDTH (SCREENWIDTH-PALETTE_WIDTH)
#define CANVAS_HEIGHT SCREENHEIGHT

#define PALETTE_WIDTH 148

Expand Down Expand Up @@ -60,7 +60,8 @@ extern app_t DrawingApp;

extern boolean_t drawing; // Are we drawing with the left mouse button?
extern boolean_t altdrawing; // Are we drawing with the right mouse button?
extern boolean_t floodFillCalled; // True if user is trying to flood fiil
extern boolean_t floodFill; // Are we flood filling with the left mouse button?
extern boolean_t floodFillCalled; // True if user is trying to flood fill
extern int mx,my; // Mouse X and Y
extern int omx, omy; // Old MouseX and MouseY (pos at previous update)
extern boolean_t mouseOnPalette; // True if the mouse is on the palette and not the canvas
Expand All @@ -71,7 +72,7 @@ extern int currentMainColor; // The selected color for LMB
extern int currentAltColor; // Color for RMB (eraser)

// Palette Buttons
#define PALETTE_BUTTONS_COUNT 13 //14
#define PALETTE_BUTTONS_COUNT 15

// All the buttons
extern button_t paletteButtons[PALETTE_BUTTONS_COUNT];
Expand Down
6 changes: 3 additions & 3 deletions elkscmd/gui/drawbmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ draw_bmp(char *path, int x, int y)
}
fclose(src);
return 1;

out:
fclose(src);
err:
Expand Down Expand Up @@ -469,8 +469,8 @@ int save_bmp(char *pathname)
if (!ofp)
return 1;

cx = SCREEN_WIDTH;
cy = SCREEN_HEIGHT;
cx = CANVAS_WIDTH;
cy = CANVAS_HEIGHT;
bpp = 8; /* write 8bpp for now */
ncolors = (bpp <= 8)? 16: 0;
bytespp = (bpp+7)/8;
Expand Down
12 changes: 12 additions & 0 deletions elkscmd/gui/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ int G_ClearScreen(struct button_s* btn)
R_ClearCanvas();
return 0;
}

int G_SetFloodFill(struct button_s* btn)
{
floodFill = true;
return 0;
}

int G_SetBrush(struct button_s* btn)
{
floodFill = false;
return 0;
}
3 changes: 3 additions & 0 deletions elkscmd/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ int G_SaveButtonOnClick(struct button_s* btn);
int G_QuitButtonOnClick(struct button_s* btn);
int G_SetBushSize(struct button_s* btn);
int G_ClearScreen(struct button_s* btn);
int G_SetFloodFill(struct button_s* btn);
int G_SetBrush(struct button_s* btn);


#endif
Binary file added elkscmd/gui/images/brush.bmp
Binary file not shown.
Binary file added elkscmd/gui/images/fill.bmp
Binary file not shown.
22 changes: 13 additions & 9 deletions elkscmd/gui/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ void I_HandleInput(void)

mx = event.x;
my = event.y;
if (mx < SCREEN_WIDTH){
if(event.button == BUTTON_L)
drawing = true;
else
altdrawing = true;
if (mx < CANVAS_WIDTH){
if(floodFill) {
floodFillCalled = true;
} else {
if(event.button == BUTTON_L)
drawing = true;
else
altdrawing = true;
}
}
hidecursor();

Expand Down Expand Up @@ -133,11 +137,11 @@ void I_HandleInput(void)
my = posy;

#if UNUSED
mx = CLAMP(mx, 0, SCREEN_WIDTH + PALETTE_WIDTH);
my = CLAMP(my, 0, SCREEN_HEIGHT-1);
mx = CLAMP(mx, 0, CANVAS_WIDTH + PALETTE_WIDTH);
my = CLAMP(my, 0, CANVAS_HEIGHT-1);

omx = CLAMP(omx, 0, SCREEN_WIDTH + PALETTE_WIDTH);
omy = CLAMP(omy, 0, SCREEN_HEIGHT-1);
omx = CLAMP(omx, 0, CANVAS_WIDTH + PALETTE_WIDTH);
omy = CLAMP(omy, 0, CANVAS_HEIGHT-1);
#endif
}

Expand Down
Loading