some code to have images in scrolling text
authorMatthias Braun <matze@braunis.de>
Mon, 26 Sep 2005 10:46:13 +0000 (10:46 +0000)
committerMatthias Braun <matze@braunis.de>
Mon, 26 Sep 2005 10:46:13 +0000 (10:46 +0000)
SVN-Revision: 2788

data/images/credits/matze.jpg [new file with mode: 0644]
data/images/credits/wansti.jpg [new file with mode: 0644]
src/textscroller.cpp

diff --git a/data/images/credits/matze.jpg b/data/images/credits/matze.jpg
new file mode 100644 (file)
index 0000000..71fa7a3
Binary files /dev/null and b/data/images/credits/matze.jpg differ
diff --git a/data/images/credits/wansti.jpg b/data/images/credits/wansti.jpg
new file mode 100644 (file)
index 0000000..346d57c
Binary files /dev/null and b/data/images/credits/wansti.jpg differ
index 066997d..d76342a 100644 (file)
@@ -66,6 +66,7 @@ void display_text_file(const std::string& filename)
   std::string text;
   std::string background_file;
   std::vector<std::string> lines;
+  std::map<std::string, Surface*> images;
 
   lisp::Parser parser;
   try {
@@ -90,6 +91,17 @@ void display_text_file(const std::string& filename)
   // Split text string lines into a vector
   split_text(text, lines);
 
+  for(size_t i = 0; i < lines.size(); ++i) {
+    const std::string& line = lines[i];
+    if(line.size() == 0)
+      continue;
+    if(line[0] == '!') {
+      std::string imagename = line.substr(1, line.size()-1);
+      std::cout << "Imagename: " << imagename << "\n";
+      images.insert(std::make_pair(imagename, new Surface(imagename, true)));
+    }
+  }
+
   // load background image
   Surface* background 
     = new Surface("images/background/" + background_file, false);
@@ -138,6 +150,7 @@ void display_text_file(const std::string& filename)
       }
       
       const Font* font = 0;
+      const Surface* image = 0;
       bool center = true;
       switch(line[0])
       {
@@ -146,26 +159,38 @@ void display_text_file(const std::string& filename)
         case '-': font = heading_font; break;
         case '*': font = reference_font; break;
         case '#': font = normal_font; center = false; break;
+        case '!': {
+            std::string imagename = line.substr(1, line.size()-1);
+            image = images[imagename];
+            break;
+        }
         default:
           std::cerr << "Warning: text contains an unformated line.\n";
           font = normal_font;
           center = false;
           break;
       }
-      
-      if(center) {
-        context.draw_text(font,
-                          line.substr(1, line.size()-1),
-                          Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
-                          CENTER_ALLIGN, LAYER_FOREGROUND1);
-      } else {
-        context.draw_text(font,
-                          line.substr(1, line.size()-1),
-                          Vector(left_border, SCREEN_HEIGHT + y - scroll),
-                          LEFT_ALLIGN, LAYER_FOREGROUND1);
+     
+      if(font != 0) {
+        if(center) {
+          context.draw_text(font,
+              line.substr(1, line.size()-1),
+              Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
+              CENTER_ALLIGN, LAYER_FOREGROUND1);
+        } else {
+          context.draw_text(font,
+              line.substr(1, line.size()-1),
+              Vector(left_border, SCREEN_HEIGHT + y - scroll),
+              LEFT_ALLIGN, LAYER_FOREGROUND1);
+        }
+        y += font->get_height() + ITEMS_SPACE;
+      }
+      if(image != 0) {
+        context.draw_surface(image,
+            Vector( (SCREEN_WIDTH - image->w) / 2,
+                    SCREEN_HEIGHT + y - scroll), 255);
+        y += image->h + ITEMS_SPACE;
       }
-      
-      y += font->get_height() + ITEMS_SPACE;
     }
     
     context.do_drawing();
@@ -182,7 +207,11 @@ void display_text_file(const std::string& filename)
     
     SDL_Delay(10);
   }
-  
+
+  for(std::map<std::string, Surface*>::iterator i = images.begin();
+      i != images.end(); ++i)
+    delete i->second;
+
   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
   delete background;
 }