breakout.pp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. program breakout;
  2. {$mode objfpc}
  3. uses glib,gdk,gtk,blocks;
  4. Type
  5. TBreakOutWindow = Class(TObject)
  6. Public
  7. window,
  8. area : PGtkWidget;
  9. BreakOut : TBreakOut;
  10. end;
  11. Var
  12. GameWindow : TBreakOutWindow;
  13. Function close_application( widget : PGtkWidget ;
  14. event : PGdkEvent;
  15. data : gpointer) : boolean; cdecl;
  16. Begin
  17. gtk_main_quit();
  18. close_application := false;
  19. End;
  20. function Exposed(Widget: PGtkWidget;event : PGdkEventExpose; Data : gpointer) : Integer; cdecl;
  21. begin
  22. { gdk_window_clear_area (widget^.window,
  23. event^.area.x,
  24. event^.area.y,
  25. event^.area.width,
  26. event^.area.height);
  27. } TBreakOutWindow(Data).BreakOut.Draw(Event);
  28. result:=0;
  29. end;
  30. function KeyPress (Widget: PGtkWidget;event : PGdkEventKey; Data : gpointer) : Integer; cdecl;
  31. begin
  32. with TBreakOutWindow(Data).BreakOut do
  33. Case event^.keyval of
  34. gdk_left : Pad.Goleft;
  35. gdk_right : Pad.GoRight;
  36. gdk_down : Pad.Stop;
  37. Ord(' ') : NextBall;
  38. end;
  39. Result:=0;
  40. end;
  41. function Step (data : Gpointer): integer;cdecl;
  42. Var
  43. Rect : TGdkRectangle;
  44. begin
  45. With TBreakOutWindow(Data) do
  46. begin
  47. With Breakout do
  48. begin
  49. Step;
  50. Draw(Nil);
  51. end;
  52. end;
  53. Result:=integer(True);
  54. end;
  55. Begin
  56. // Initialize GTK and create the main window
  57. gtk_init( @argc, @argv );
  58. GameWindow:=TBreakOutWindow.Create;
  59. With GameWindow do
  60. begin
  61. window := gtk_window_new( GTK_WINDOW_TOPLEVEL );
  62. gtk_window_set_policy(PgtkWindow(Window),0,0,1);
  63. gtk_signal_connect (GTK_OBJECT (window), 'delete_event',
  64. GTK_SIGNAL_FUNC( @close_application ), NIL);
  65. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  66. area := gtk_drawing_area_new();
  67. gtk_container_add( GTK_CONTAINER(window), Area);
  68. BreakOut:=TBreakOut.Create(area);
  69. With BreakOut.BlockList do
  70. begin
  71. TotalRows:=20;
  72. TotalColumns:=10;
  73. StartRow:=15;
  74. BlockRows:=5;
  75. BlockSpacing:=2;
  76. end;
  77. gtk_signal_connect (GTK_OBJECT (area),'expose_event',
  78. GTK_SIGNAL_FUNC(@Exposed),GameWindow);
  79. gtk_drawing_area_size (PGTKDRAWINGAREA(area),600,400);
  80. // key handling.
  81. gtk_widget_set_events(window,GDK_KEY_RELEASE_MASK);
  82. gtk_signal_connect(PGTKObject(Window),'key_press_event',
  83. GTK_SIGNAL_FUNC(@KeyPress),GameWindow);
  84. // Timer handling.
  85. gtk_timeout_add(50,@Step,GameWindow);
  86. gtk_widget_show_all( window );
  87. gtk_main();
  88. end;
  89. End.
  90. end.