SSJX.CO.UK
Content

Simple image transition program

This program displays an image which is then slowly replaced using a cool transition effect. We do this by creating an array of 0-3999, randomising it and then use this random order to replace the screen bytes with the bytes from another image.

You can download the source using the link below, the actual .app is not included as you will need to specify two image files for the program to use (lines 64 and 67). Once this is done, just run or double click on make.bat to create the pictran.app file.

Download now

What you will need

The program

Note: Don't type in the line numbers!

  1. //
  2. // Simple picture transition by ssjx (04/08/2006)
  3. //
  4. #include <cybiko.h>
  5. struct module_t main_module;
  6. struct FileInput file_input;
  7. void loadpic(char *filename,char *dest)
  8. {
  9. FileInput_ctor(&file_input);
  10. if(FileInput_open( &file_input,filename))
  11. {
  12.     TRACE("Loading picture: %s",filename);
  13.     FileInput_seek( &file_input, 8, SEEK_SET );
  14.     Input_read( &file_input,dest,4000);
  15. }
  16. else
  17. {
  18.     TRACE("Could not open file: %s.",filename);
  19. }
  20. FileInput_dtor( &file_input, LEAVE_MEMORY);
  21. }
  22. long main(int argc, char *argv[],bool start)
  23. {
  24. char *dis;
  25. char *pic;
  26. int *order;
  27. int i;
  28. int a,b,c;
  29. init_module( &main_module );
  30. dis=DisplayGraphics_get_page_ptr(main_module.m_gfx, 0);
  31. pic=(char *)malloc(4000);
  32. order=(int *)calloc(4000,sizeof(int));
  33. if (pic!=0 && order!=0)
  34. {
  35.     TRACE("Allocated required memory!");
  36.     for(i=0;i<4000;i++)
  37.     {
  38.     order[i]=i;
  39.     }
  40.     
  41.     for(i=0;i<4000;i++)
  42.     {
  43.     a=random(4000);
  44.     b=random(4000);
  45.     
  46.     c=order[a];
  47.     order[a]=order[b];
  48.     order[b]=c;
  49.     }
  50.     TRACE("Loading images!");
  51.     loadpic("pic1.pic",dis);
  52.     DisplayGraphics_show(main_module.m_gfx);
  53.     loadpic("pic2.pic",pic);
  54.     TRACE("Displaying!");
  55.     for(i=0;i<4000;i++)
  56.     {
  57.     dis[order[i]]=pic[order[i]];
  58.     DisplayGraphics_show(main_module.m_gfx);
  59.     }
  60. }
  61. else
  62. {
  63. TRACE("Could not allocate memory");
  64. }
  65. free(pic);
  66. free(order);
  67. return 0l;
  68. }

How it works

Line(s)
5-6 The standard Cybiko include and main module required by most Cybiko programs.
8 We are going to be using files so we need this system struct to hold the file details.
10-26 As we are loading two images later, it is best to put the could into a function we can re-use. If we can open the file, we skip the first 8 bytes and copy the remaining 4000 (which is the actual raw image) to the area of memory pointed to by the 'dest' variable.
30-34 Variable we will need. *pic and *dis are pointers to the hidden picture and the actual display. *order points to the list of bytes that get replaced.
36 Initialises the main_module struct (I guess). Another 'needed by most Cybiko programs' line.
38 Set the display pointer 'dis' to point to the start of the display data.
40 Allocated space for our hidden picture. This is pointed to by 'pic'.
41 This allocated the 4000 integers we will need. In most 'c' programs for other systems, we would just use a large array (e.g. int order[4000];) but the Cybiko sdk does not like large amounts of memory being allocated like that, so we use malloc or calloc instead. We must remember to free() these when we are done though, this gives the memory back to the system.
43 If pic or order equal zero, it means we were not able to allocate the memory needed. If this is the case we give an error message and end.
47-50 This fills the order array with the values 0-3999. We will change the order of these values so that when we display the new image, it appears by a random pattern.
52-60 This randomises the order array. It picks two values at random and then swaps them. We will read through this array and use the values to determine which bytes to replace
64-65 Using our 'loadpic' function, we load the image to the display memory and then show it. Make sure this image file is one that is on your Cybiko's flash memory.
67 This loads our other image into the memory block we allocated earlier. This also needs to be an image on your Cybiko's flash.
69-74 To show the new image, we read through the order array and use the value to replace the pixel blocks. Each image byte holds the bits for four pixels. We call the show function to update the display after each change.
83-84 Before exiting, we free the memory we allocated at the start. Very important and also very easy to forget...

Updated 20/04/2024