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
If you have any comments about this tutorial, please use the DevCybiko /
PlanetCybiko forums or personal messaging
and let me know!
What you will need
The Cybiko SDK
Two cybiko .pic files which are 160x100 (4008 bytes each)
The program
Note: Don't type in the line numbers!
- //
- // Simple picture transition by ssjx (4/8/6)
- //
-
- #include <cybiko.h>
- struct module_t main_module;
-
- struct FileInput file_input;
-
- void loadpic(char *filename,char *dest)
- {
- FileInput_ctor(&file_input);
-
- if(FileInput_open( &file_input,filename))
- {
- TRACE("Loading picture: %s",filename);
- FileInput_seek( &file_input, 8, SEEK_SET );
- Input_read( &file_input,dest,4000);
- }
- else
- {
- TRACE("Could not open file: %s.",filename);
- }
-
- FileInput_dtor( &file_input, LEAVE_MEMORY);
- }
-
- long main(int argc, char *argv[],bool start)
- {
- char *dis;
- char *pic;
- int *order;
- int i;
- int a,b,c;
-
- init_module( &main_module );
-
- dis=DisplayGraphics_get_page_ptr(main_module.m_gfx, 0);
-
- pic=(char *)malloc(4000);
- order=(int *)calloc(4000,sizeof(int));
-
- if (pic!=0 && order!=0)
- {
- TRACE("Allocated required memory!");
-
- for(i=0;i<4000;i++)
- {
- order[i]=i;
- }
-
- for(i=0;i<4000;i++)
- {
- a=random(4000);
- b=random(4000);
-
- c=order[a];
- order[a]=order[b];
- order[b]=c;
- }
-
- TRACE("Loading images!");
-
- loadpic("pic1.pic",dis);
- DisplayGraphics_show(main_module.m_gfx);
-
- loadpic("pic2.pic",pic);
-
- TRACE("Displaying!");
- for(i=0;i<4000;i++)
- {
- dis[order[i]]=pic[order[i]];
- DisplayGraphics_show(main_module.m_gfx);
- }
-
-
- }
- else
- {
- TRACE("Could not allocate memory");
- }
-
- free(pic);
- free(order);
-
- return 0l;
- }
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 poiner '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 thouhgh, 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 cybikos flash memory.
|
| 67
| This loads our other image into the memory block we allocated earlier. This also needs to be an image on your
cybikos 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 exitting, we free the memory we allocated at the start. Very important and also very easy to forget...
|
|
|