From: Holger Schemel <holger.schemel@virtion.de>
Date: Thu, 3 Oct 2024 14:20:32 +0000 (+0200)
Subject: changed BD style color template support to cover all images sizes
X-Git-Tag: 4.4.0.0-test-4~180
X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=9bd692aaff78aa8582415cda3950edf7190f0ba2;p=rocksndiamonds.git

changed BD style color template support to cover all images sizes

The new method uses the newly added ".color_template" parameter in the
"graphicsinfo.conf" config file to mark an image as "color template".
In the level editor, all color changes are immediately applied to the
game elements selection list.
---

diff --git a/src/editor.c b/src/editor.c
index 88fdbccc..a09fd29c 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -10087,6 +10087,7 @@ static void DrawEditModeWindowExt(boolean remap_toolbox_gadgets)
   if (remap_toolbox_gadgets)
   {
     SetCurrentLevelColors_BD();
+    InitColorTemplateImagesIfNeeded();
 
     ModifyEditorElementList();
     RedrawDrawingElements();
@@ -11830,7 +11831,7 @@ static void DrawEngineConfigColors(void)
 {
   int i;
 
-  if (!hasColorTemplate_BD())
+  if (!hasColorTemplate())
   {
     int font_nr = FONT_TEXT_1;
     int font_height = getFontHeight(font_nr);
diff --git a/src/files.c b/src/files.c
index e999cb6c..74fc442e 100644
--- a/src/files.c
+++ b/src/files.c
@@ -8133,6 +8133,8 @@ static void LoadLevel_LoadAndInit(struct NetworkLevelInfo *network_level)
   LoadLevel_InitSettings(&level);
 
   LoadLevel_InitNativeEngines(&level);
+
+  InitColorTemplateImagesIfNeeded();
 }
 
 void LoadLevel(int nr)
diff --git a/src/game_bd/bd_graphics.c b/src/game_bd/bd_graphics.c
index 5f1c01d8..873efad8 100644
--- a/src/game_bd/bd_graphics.c
+++ b/src/game_bd/bd_graphics.c
@@ -542,6 +542,14 @@ Bitmap *gd_get_tile_bitmap(Bitmap *bitmap)
   return bitmap;
 }
 
+Bitmap *gd_get_colored_bitmap_from_template(Bitmap *template_bitmap)
+{
+  SDL_Surface *template_surface = get_tile_surface_c64(template_bitmap->surface, 1);
+  Bitmap *colored_bitmap = get_tile_bitmap_c64(native_bd_level.cave, template_surface);
+
+  return colored_bitmap;
+}
+
 // returns true if the element has a certain property
 static inline boolean has_property(const int element, const int property)
 {
diff --git a/src/game_bd/bd_graphics.h b/src/game_bd/bd_graphics.h
index e52048ad..0799b6c7 100644
--- a/src/game_bd/bd_graphics.h
+++ b/src/game_bd/bd_graphics.h
@@ -37,6 +37,7 @@ boolean gd_bitmap_has_c64_colors(Bitmap *bitmap);
 void gd_prepare_tile_bitmap(GdCave *cave, Bitmap *bitmap, int scale_down_factor);
 void gd_set_tile_bitmap_reference(Bitmap *bitmap);
 Bitmap *gd_get_tile_bitmap(Bitmap *bitmap);
+Bitmap *gd_get_colored_bitmap_from_template(Bitmap *template_bitmap);
 
 int gd_drawcave(Bitmap *dest, GdGame *gameplay, boolean);
 boolean gd_scroll(GdGame *gameplay, boolean exact_scroll, boolean immediate);
diff --git a/src/game_bd/export_bd.h b/src/game_bd/export_bd.h
index 0e6cb948..65a06c95 100644
--- a/src/game_bd/export_bd.h
+++ b/src/game_bd/export_bd.h
@@ -133,6 +133,7 @@ void DumpLevelset_BD(void);
 void PreparePreviewTileBitmap_BD(Bitmap *, int);
 void SetPreviewTileBitmapReference_BD(Bitmap *);
 Bitmap *GetPreviewTileBitmap_BD(Bitmap *);
+Bitmap *GetColoredBitmapFromTemplate_BD(Bitmap *);
 
 unsigned int InitEngineRandom_BD(int);
 void InitGameEngine_BD(void);
diff --git a/src/game_bd/main_bd.c b/src/game_bd/main_bd.c
index ab122bfb..0912b417 100644
--- a/src/game_bd/main_bd.c
+++ b/src/game_bd/main_bd.c
@@ -314,6 +314,11 @@ Bitmap *GetPreviewTileBitmap_BD(Bitmap *bitmap)
   return gd_get_tile_bitmap(bitmap);
 }
 
+Bitmap *GetColoredBitmapFromTemplate_BD(Bitmap *bitmap)
+{
+  return gd_get_colored_bitmap_from_template(bitmap);
+}
+
 unsigned int InitEngineRandom_BD(int seed)
 {
   if (seed == NEW_RANDOMIZE)
diff --git a/src/init.c b/src/init.c
index 0cfe53b3..d93486c6 100644
--- a/src/init.c
+++ b/src/init.c
@@ -344,6 +344,42 @@ static void InitBitmapPointers(void)
       graphic_info[i].bitmap = graphic_info[i].bitmaps[IMG_BITMAP_STANDARD];
 }
 
+boolean hasColorTemplate(void)
+{
+  int num_images = getImageListSize();
+  int i;
+
+  for (i = 0; i < num_images; i++)
+    if (graphic_info[i].color_template)
+      return TRUE;
+
+  return FALSE;
+}
+
+void InitColorTemplateImages(void)
+{
+  int num_images = getImageListSize();
+  int i;
+
+  // if graphic is marked as "color template", reset using colored bitmaps
+  for (i = 0; i < num_images; i++)
+    if (graphic_info[i].color_template)
+      ResetColorTemplateImage(i);
+
+  // if graphic is marked as "color template", re-color all scaled bitmaps
+  for (i = 0; i < num_images; i++)
+    if (graphic_info[i].color_template)
+      CreateImgesFromColorTemplate(i, GetColoredBitmapFromTemplate_BD);
+
+  InitImageTextures();
+}
+
+void InitColorTemplateImagesIfNeeded(void)
+{
+  if (hasColorTemplate())
+    InitColorTemplateImages();
+}
+
 void InitImageTextures(void)
 {
   static int texture_graphics[] =
diff --git a/src/init.h b/src/init.h
index 8c068587..65f21ddf 100644
--- a/src/init.h
+++ b/src/init.h
@@ -40,6 +40,10 @@ void InitGfxBuffers(void);
 void InitGadgets(void);
 void InitImageTextures(void);
 
+boolean hasColorTemplate(void);
+void InitColorTemplateImages(void);
+void InitColorTemplateImagesIfNeeded(void);
+
 void InitNetworkServer(void);
 
 void DisplayExitMessage(char *, va_list);
diff --git a/src/libgame/image.c b/src/libgame/image.c
index 30859c3b..ad9dfde8 100644
--- a/src/libgame/image.c
+++ b/src/libgame/image.c
@@ -407,6 +407,61 @@ void ScaleImage(int pos, int zoom_factor)
   }
 }
 
+void ResetColorTemplateImage(int pos)
+{
+  ImageInfo *img_info = getImageInfoEntryFromImageID(pos);
+
+  if (img_info->template == NULL)
+  {
+    // image color template not yet defined -- copy from original bitmap
+    Bitmap *orig_bitmap = img_info->bitmaps[IMG_BITMAP_PTR_ORIGINAL];
+
+    img_info->template = ZoomBitmap(orig_bitmap, orig_bitmap->width, orig_bitmap->height);
+  }
+
+  img_info->contains_color_images = FALSE;
+}
+
+void CreateImgesFromColorTemplate(int pos, Bitmap * (*color_bitmap_function)(Bitmap *))
+{
+  ImageInfo *img_info = getImageInfoEntryFromImageID(pos);
+  int i;
+
+  if (img_info->contains_color_images)
+    return;
+
+  Bitmap *colored_bitmap = color_bitmap_function(img_info->template);
+
+  if (colored_bitmap != NULL)
+  {
+    // (re-)create all bitmaps from color template bitmap
+    for (i = 0; i < NUM_IMG_BITMAPS; i++)
+    {
+      Bitmap *old_bitmap = img_info->bitmaps[i];
+
+      if (old_bitmap == NULL)
+        continue;
+
+      // create new surfaces (with previous size) (this does not create textures)
+      Bitmap *new_bitmap = ZoomBitmap(colored_bitmap, old_bitmap->width, old_bitmap->height);
+
+      // free old surfaces
+      SDL_FreeSurface(old_bitmap->surface);
+      SDL_FreeSurface(old_bitmap->surface_masked);
+
+      // copy newly created surfaces from new bitmap
+      old_bitmap->surface        = new_bitmap->surface;
+      old_bitmap->surface_masked = new_bitmap->surface_masked;
+
+      // free empty new bitmap structure
+      checked_free(new_bitmap);
+    }
+  }
+
+  // (also set if coloring failed, to prevent repeating for all graphics using this image)
+  img_info->contains_color_images = TRUE;
+}
+
 void FreeAllImages(void)
 {
   FreeCustomArtworkLists(image_info);
diff --git a/src/libgame/image.h b/src/libgame/image.h
index 04952e74..8e559315 100644
--- a/src/libgame/image.h
+++ b/src/libgame/image.h
@@ -89,6 +89,9 @@ void CreateImageTextures(int);
 void FreeAllImageTextures(void);
 void ScaleImage(int, int);
 
+void ResetColorTemplateImage(int);
+void CreateImgesFromColorTemplate(int, Bitmap * (*color_bitmap_function)(Bitmap *));
+
 void FreeAllImages(void);
 
 #endif	// IMAGE_H