[gnome-robots] Create Theme class



commit 9be314f5575b8860243ed0b1340a2b0ca381932c
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sat Sep 12 16:32:06 2020 +0200

    Create Theme class

 src/game-area.vala |   1 -
 src/graphics.vala  | 106 +++++++++++++----------------------------------------
 src/meson.build    |   1 +
 src/theme.vala     |  83 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 109 insertions(+), 82 deletions(-)
---
diff --git a/src/game-area.vala b/src/game-area.vala
index 3675d02..3863be2 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -53,7 +53,6 @@ public class GameArea : DrawingArea {
         if (trial_width != tile_width || trial_height != tile_height) {
             tile_width = trial_width;
             tile_height = trial_height;
-            rerender_needed = true;
         }
 
         return false;
diff --git a/src/graphics.vala b/src/graphics.vala
index 5d39dfb..bca4f9a 100644
--- a/src/graphics.vala
+++ b/src/graphics.vala
@@ -39,26 +39,13 @@ public const int BUBBLE_HEIGHT = 34;
 public const int BUBBLE_XOFFSET = 8;
 public const int BUBBLE_YOFFSET = 4;
 
-/*
- * Scenario pixmaps
- */
-public const int SCENARIO_PIXMAP_WIDTH = 14;
-public const int SCENARIO_PLAYER_START = 0;
-public const int SCENARIO_ROBOT1_START = 5;
-public const int SCENARIO_ROBOT2_START = 9;
-public const int SCENARIO_HEAP_POS     = 13;
-
-public const int NUM_ROBOT_ANIMATIONS  = 4;
-public const int NUM_PLAYER_ANIMATIONS = 4;
 public const int PLAYER_WAVE_WAIT      = 20;
 public const int PLAYER_NUM_WAVES      = 2;
 
 public int tile_width = 0;
 public int tile_height = 0;
 
-GamesPreimage theme_preimage = null;
-Pixbuf theme_pixbuf = null;
-bool rerender_needed = true;
+Theme theme = null;
 
 RGBA light_background;
 RGBA dark_background;
@@ -79,11 +66,6 @@ int bubble_xo = 0;
 int bubble_yo = 0;
 BubbleType bubble_type = BubbleType.NONE;
 
-void render_graphics () {
-    theme_pixbuf = theme_preimage.render (14 * tile_width, tile_height);
-    rerender_needed = false;
-}
-
 /**
  * Loads all of the 'speech bubble' graphics
  **/
@@ -107,26 +89,9 @@ void load_bubble_graphics () throws Error {
  * TRUE on success FALSE otherwise
  **/
 public void load_game_graphics (string theme_path) throws Error {
-    if (theme_preimage != null) {
-        free_game_graphics ();
-    }
-
-    theme_preimage = new GamesPreimage.from_file (theme_path);
+    theme = new Theme.from_file (theme_path);
 
     load_bubble_graphics ();
-
-    rerender_needed = true;
-}
-
-/**
- * Frees all of the resources used by the game graphics
- **/
-public void free_game_graphics () {
-    theme_preimage = null;
-    theme_pixbuf = null;
-    aieee_pixbuf = null;
-    yahoo_pixbuf = null;
-    splat_pixbuf = null;
 }
 
 public void set_background_color (RGBA color) {
@@ -168,18 +133,19 @@ public void set_background_color_from_name (string name) {
 }
 
 /**
- * draw_tile_pixmap
- * @tileno: Graphics tile number
- * @pno: Number of graphics set
- * @x: x position in grid squares
- * @y: y position in grid squares
+ * draw_object
+ * @x: x position
+ * @y: y position
+ * @type: object type
  * @cr: context to draw on
  *
  * Description:
- * Draws tile pixmap @tileno from graphics set @pno at (@x, @y) in
- * a widget @area
+ * Draws graphics for an object at specified location
  **/
-void draw_tile_pixmap (int tileno, int x, int y, Context cr) {
+public void draw_object (int x, int y, ObjectType type, Context cr) {
+    if (game_area == null)
+        return;
+
     if ((x + y) % 2 != 0) {
         cairo_set_source_rgba (cr, dark_background);
     } else {
@@ -192,50 +158,28 @@ void draw_tile_pixmap (int tileno, int x, int y, Context cr) {
     cr.rectangle (x, y, tile_width, tile_height);
     cr.fill ();
 
-    if (rerender_needed)
-        render_graphics ();
-
-    if ((tileno < 0) || (tileno >= SCENARIO_PIXMAP_WIDTH)) {
-        /* nothing */
-    } else {
-        cairo_set_source_pixbuf (cr, theme_pixbuf, x - tileno * tile_width, y);
-        cr.rectangle (x, y, tile_width, tile_height);
-        cr.fill ();
-    }
-}
-
-
-/**
- * draw_object
- * @x: x position
- * @y: y position
- * @type: object type
- * @cr: context to draw on
- *
- * Description:
- * Draws graphics for an object at specified location
- **/
-public void draw_object (int x, int y, ObjectType type, Context cr) {
-    if (game_area == null)
-        return;
-
+    int animation = 0;
     switch (type) {
     case ObjectType.PLAYER:
-        draw_tile_pixmap (SCENARIO_PLAYER_START + player_animation, x, y, cr);
+        animation = player_animation;
         break;
     case ObjectType.ROBOT1:
-        draw_tile_pixmap (SCENARIO_ROBOT1_START + robot_animation, x, y, cr);
+        animation = robot_animation;
         break;
     case ObjectType.ROBOT2:
-        draw_tile_pixmap (SCENARIO_ROBOT2_START + robot_animation, x, y, cr);
+        animation = robot_animation;
         break;
     case ObjectType.HEAP:
-        draw_tile_pixmap (SCENARIO_HEAP_POS, x, y, cr);
+        animation = 0;
         break;
     case ObjectType.NONE:
-        draw_tile_pixmap (-1, x, y, cr);
         break;
     }
+
+    cr.save ();
+    cr.translate (x, y);
+    theme.draw_object (type, animation, cr, tile_width, tile_height);
+    cr.restore ();
 }
 
 
@@ -274,7 +218,7 @@ public void player_animation_dead () {
     player_wave_wait = 0;
     player_num_waves = 0;
     player_wave_dir = 1;
-    player_animation = NUM_PLAYER_ANIMATIONS;
+    player_animation = Theme.Frames.NUM_PLAYER_ANIMATIONS;
 }
 
 
@@ -286,18 +230,18 @@ public void player_animation_dead () {
  **/
 public void animate_game_graphics () {
   ++robot_animation;
-  if (robot_animation >= NUM_ROBOT_ANIMATIONS) {
+  if (robot_animation >= Theme.Frames.NUM_ROBOT1_ANIMATIONS) {
     robot_animation = 0;
   }
 
-  if (player_animation == NUM_PLAYER_ANIMATIONS) {
+  if (player_animation == Theme.Frames.NUM_PLAYER_ANIMATIONS) {
     /* do nothing */
   } else if (player_wave_wait < PLAYER_WAVE_WAIT) {
     ++player_wave_wait;
     player_animation = 0;
   } else {
     player_animation += player_wave_dir;
-    if (player_animation >= NUM_PLAYER_ANIMATIONS) {
+    if (player_animation >= Theme.Frames.NUM_PLAYER_ANIMATIONS) {
       player_wave_dir = -1;
       player_animation -= 2;
     } else if (player_animation < 0) {
diff --git a/src/meson.build b/src/meson.build
index 8d415ed..75e660f 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -13,6 +13,7 @@ sources = files(
 
     'image-suffix-list.vala',
     'themes.vala',
+    'theme.vala',
     'preimage.vala',
     'controls.vala',
     'game-config.vala',
diff --git a/src/theme.vala b/src/theme.vala
new file mode 100644
index 0000000..7a18891
--- /dev/null
+++ b/src/theme.vala
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2020 Andrey Kutejko <andy128k gmail com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * For more details see the file COPYING.
+ */
+
+using Gdk;
+using Cairo;
+
+public class Theme {
+
+    public enum Frames {
+        PLAYER_START = 0,
+        ROBOT1_START = 5,
+        ROBOT2_START = 9,
+        HEAP_START = 13,
+        COUNT = 14,
+
+        NUM_PLAYER_ANIMATIONS = ROBOT1_START - PLAYER_START,
+        NUM_ROBOT1_ANIMATIONS = ROBOT2_START - ROBOT1_START,
+        NUM_ROBOT2_ANIMATIONS = HEAP_START - ROBOT2_START,
+        NUM_HEAP_ANIMATIONS = COUNT - HEAP_START,
+    }
+
+    private GamesPreimage preimage;
+    private Pixbuf pixbuf;
+    private int tile_width;
+    private int tile_height;
+
+    public Theme.from_file (string path) throws Error {
+        preimage = new GamesPreimage.from_file (path);
+        pixbuf = null;
+    }
+
+    public void draw_object (ObjectType type,
+                             int frame_no,
+                             Context cr,
+                             int width,
+                             int height
+    ) {
+        int tile_no = -1;
+        switch (type) {
+        case ObjectType.PLAYER:
+            tile_no = Frames.PLAYER_START + frame_no % Frames.NUM_PLAYER_ANIMATIONS;
+            break;
+        case ObjectType.ROBOT1:
+            tile_no = Frames.ROBOT1_START + frame_no % Frames.NUM_ROBOT1_ANIMATIONS;
+            break;
+        case ObjectType.ROBOT2:
+            tile_no = Frames.ROBOT2_START + frame_no % Frames.NUM_ROBOT2_ANIMATIONS;
+            break;
+        case ObjectType.HEAP:
+            tile_no = Frames.HEAP_START + frame_no % Frames.NUM_HEAP_ANIMATIONS;
+            break;
+        case ObjectType.NONE:
+            return;
+        }
+
+        if (pixbuf == null || tile_width != width || tile_height != height) {
+            tile_width = width;
+            tile_height = height;
+            pixbuf = preimage.render (Frames.COUNT * tile_width, tile_height);
+        }
+
+        cairo_set_source_pixbuf (cr, pixbuf, - tile_no * tile_width, 0);
+        cr.rectangle (0, 0, tile_width, tile_height);
+        cr.fill ();
+    }
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]