@@ -295,8 +295,12 @@ def transpile(self, code: str, lang: str = "c") -> str:
295295 '' ,
296296 '#include <stdio.h>' ,
297297 '#include <string.h>' ,
298+ '#include <SDL.h>' , # Include SDL for windowing
299+ 'SDL_Window* window;' ,
300+ 'SDL_Renderer* renderer;' ,
301+ '' ,
302+ # Declare global string variable
298303 'char str[256] = "";' ,
299- '#include <SDL.h>' ,
300304 ''
301305 ]
302306 self .global_vars .clear ()
@@ -310,6 +314,10 @@ def transpile(self, code: str, lang: str = "c") -> str:
310314 self .indent_level = 1
311315 self .block_stack .clear ()
312316 self .vars .clear ()
317+ self .draw_code = []
318+ window_width = 640
319+ window_height = 480
320+ window_title = "BScript Window"
313321
314322 i = 0
315323 while i < len (lines ):
@@ -551,28 +559,72 @@ def transpile(self, code: str, lang: str = "c") -> str:
551559 i += 1
552560 continue
553561
554- # Window
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+
579+ # windowSize command
580+ m = re .match (r'windowSize\s+(\d+)\s*,\s*(\d+);' , line )
581+ if m :
582+ width = int (m .group (1 ))
583+ height = int (m .group (2 ))
584+
585+ if not (0 <= width <= 255 ) or not (0 <= height <= 255 ):
586+ raise Exception (f"Invalid window size: { width } x{ height } . Each dimension must be between 0 and 255 pixels." )
587+
588+ window_width = width * 3
589+ window_height = height * 3
590+ i += 1
591+ continue
592+
593+
594+ # windowTitle command
595+ m = re .match (r'windowTitle\s+"([^"]*)";' , line )
596+ if m :
597+ window_title = m .group (1 )
598+ i += 1
599+ continue
600+
601+ # window command
555602 if line == 'window;' :
556603 if windowed :
557604 raise Exception ("Window already created, cannot create another" )
558- continue
559-
560605 windowed = True
606+
561607 sdl_code = [
562608 "int main(int argc, char* argv[]) {" ,
563609 " if (SDL_Init(SDL_INIT_VIDEO) != 0) {" ,
564610 " printf(\" SDL_Init Error: %s\\ n\" , SDL_GetError());" ,
565611 " return 1;" ,
566612 " }" ,
567- " SDL_Window* window = SDL_CreateWindow(\" BScript Window \" ," ,
613+ f " SDL_Window* window = SDL_CreateWindow(\" { window_title } \" ," ,
568614 " SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED," ,
569- " 640, 480 , SDL_WINDOW_SHOWN);" ,
615+ f " { window_width } , { window_height } , SDL_WINDOW_SHOWN);" ,
570616 " if (!window) {" ,
571617 " printf(\" SDL_CreateWindow Error: %s\\ n\" , SDL_GetError());" ,
572618 " SDL_Quit();" ,
573619 " return 1;" ,
574620 " }" ,
575-
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+ " }" ,
576628 " SDL_Event e;" ,
577629 " int running = 1;" ,
578630 " while (running) {" ,
@@ -581,16 +633,25 @@ def transpile(self, code: str, lang: str = "c") -> str:
581633 " running = 0;" ,
582634 " }" ,
583635 " }" ,
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);" ,
584644 " SDL_Delay(16);" ,
585645 " }" ,
586-
646+ " SDL_DestroyRenderer(renderer);" ,
587647 " SDL_DestroyWindow(window);" ,
588648 " SDL_Quit();"
589649 ]
590- if self .in_function :
591- self .c_lines .extend (f'{ self .indent ()} { stmt } ' for stmt in sdl_code )
592- else :
593- 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)
594655 i += 1
595656 continue
596657
@@ -608,8 +669,12 @@ def transpile(self, code: str, lang: str = "c") -> str:
608669 output .append (" return 0;" )
609670 output .append ("}" )
610671 else :
672+ output .extend (sdl_code )
611673 # 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" )
612676 output .extend (self .main_code )
677+ output .extend (sdl_code2 )
613678 output .append ("}" )
614679
615680 return '\n ' .join (output )
@@ -650,4 +715,3 @@ def compile(self, source_file: str, output_name: str):
650715 raise RuntimeError (f"Compilation failed: { e } " )
651716
652717 print (f"Compiled { source_file } to { output_name } " )
653-
0 commit comments