Skip to content

Commit 942fef5

Browse files
committed
wip graphical stuff
1 parent b3b51bf commit 942fef5

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

1.39 KB
Binary file not shown.

compiler.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ def transpile(self, code: str, lang: str = "c") -> str:
296296
'#include <stdio.h>',
297297
'#include <string.h>',
298298
'#include <SDL.h>', # Include SDL for windowing
299+
'SDL_Window* window;',
300+
'SDL_Renderer* renderer;',
299301
'',
300302
# Declare global string variable
301303
'char str[256] = "";',
@@ -312,6 +314,7 @@ def transpile(self, code: str, lang: str = "c") -> str:
312314
self.indent_level = 1
313315
self.block_stack.clear()
314316
self.vars.clear()
317+
self.draw_code = []
315318
window_width = 640
316319
window_height = 480
317320
window_title = "BScript Window"
@@ -556,6 +559,23 @@ def transpile(self, code: str, lang: str = "c") -> str:
556559
i += 1
557560
continue
558561

562+
# Pixel drawing command
563+
m = re.match(r'pixel\s+(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+);', line)
564+
if m:
565+
x, y, r, g, b = map(int, m.groups())
566+
draw_code = [
567+
f'{self.indent()}SDL_SetRenderDrawColor(renderer, {r}, {g}, {b}, 255);',
568+
f'{self.indent()}SDL_Rect rect = {{ {x} * 3, {y} * 3, 3, 3 }};',
569+
f'{self.indent()}SDL_RenderFillRect(renderer, &rect);'
570+
]
571+
if self.in_function:
572+
self.c_lines.extend(draw_code)
573+
else:
574+
self.draw_code.extend(draw_code)
575+
i += 1
576+
continue
577+
578+
559579
# windowSize command
560580
m = re.match(r'windowSize\s+(\d+)\s*,\s*(\d+);', line)
561581
if m:
@@ -598,6 +618,13 @@ def transpile(self, code: str, lang: str = "c") -> str:
598618
" SDL_Quit();",
599619
" return 1;",
600620
" }",
621+
" SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);",
622+
" if (!renderer) {",
623+
" printf(\"SDL_CreateRenderer Error: %s\\n\", SDL_GetError());",
624+
" SDL_DestroyWindow(window);",
625+
" SDL_Quit();",
626+
" return 1;",
627+
" }",
601628
" SDL_Event e;",
602629
" int running = 1;",
603630
" while (running) {",
@@ -606,15 +633,25 @@ def transpile(self, code: str, lang: str = "c") -> str:
606633
" running = 0;",
607634
" }",
608635
" }",
636+
" SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);",
637+
" SDL_RenderClear(renderer);",
638+
" // Begin drawing section",
639+
" // <-- BScript pixel draw commands go here -->"
640+
]
641+
sdl_code2 = [
642+
" // End drawing section",
643+
" SDL_RenderPresent(renderer);",
609644
" SDL_Delay(16);",
610645
" }",
646+
" SDL_DestroyRenderer(renderer);",
611647
" SDL_DestroyWindow(window);",
612648
" SDL_Quit();"
613649
]
614-
if self.in_function:
615-
self.c_lines.extend(f'{self.indent()}{stmt}' for stmt in sdl_code)
616-
else:
617-
self.main_code.extend(f'{self.indent()}{stmt}' for stmt in sdl_code)
650+
651+
#if self.in_function:
652+
#self.c_lines.extend(f'{self.indent()}{stmt}' for stmt in sdl_code)
653+
#else:
654+
# self.main_code.extend(f'{self.indent()}{stmt}' for stmt in sdl_code)
618655
i += 1
619656
continue
620657

@@ -632,8 +669,12 @@ def transpile(self, code: str, lang: str = "c") -> str:
632669
output.append(" return 0;")
633670
output.append("}")
634671
else:
672+
output.extend(sdl_code)
635673
# If windowed, we already have the main function in sdl_code
674+
output.extend(self.draw_code)
675+
output.append(" // End of BScript pixel draw commands")
636676
output.extend(self.main_code)
677+
output.extend(sdl_code2)
637678
output.append("}")
638679

639680
return '\n'.join(output)

tests/window

33.3 KB
Binary file not shown.

tests/windowstuff.bs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
windowSize 50,50;
2+
windowTitle "Pixel Test!";
3+
window;
4+
5+
pixel 25,25,255,255,255;

0 commit comments

Comments
 (0)