// // MAIN: Using keyboard to move a sprite example // void initialize() { u8* pvideomem; // Pointer to video memory // Disable firmware to prevent it from interfering with setPalette and setVideoMode cpct_disableFirmware(); // Set up the hardware palette using hardware colour values cpct_setPalette(g_palette, 16); cpct_setBorder(HW_BLACK); // Set video mode 0 (160x200, 16 colours) cpct_setVideoMode(0); // Draw floor. As cpct_drawSolidBox cannot draw boxes wider than 63 bytes // and Screen width is 80 bytes, we draw 2 boxes of SCR_W/2 (40 bytes) each pvideomem = cpct_getScreenPtr(CPCT_VMEM_START, 0, FLOOR_Y); cpct_drawSolidBox(pvideomem, FLOOR_COLOR, SCR_W/2, FLOOR_HEIGHT); pvideomem = cpct_getScreenPtr(CPCT_VMEM_START, SCR_W/2, FLOOR_Y); cpct_drawSolidBox(pvideomem, FLOOR_COLOR, SCR_W/2, FLOOR_HEIGHT); // Draw instructions pvideomem = cpct_getScreenPtr(CPCT_VMEM_START, 0, 20); cpct_drawStringM0(" Sprite Flip Demo ", pvideomem, 2, 0); pvideomem = cpct_getScreenPtr(CPCT_VMEM_START, 0, 34); cpct_drawStringM0("[Cursor]", pvideomem, 4, 0); pvideomem = cpct_getScreenPtr(CPCT_VMEM_START, 40, 34); cpct_drawStringM0("Left/Right", pvideomem, 3, 0); }
// // MAIN: Keyboard check example // void main(void) { // 32 bits counter, to let it count passed 65536 (up to 4 Billions) u32 i; // Disable firmware to prevent it from interfering with setVideoMode cpct_disableFirmware(); // Convert palettes from firmware colour values to // hardware colours (which are used by cpct_setPalette) cpct_fw2hw(G_banner_palette, 16); cpct_fw2hw(G_logo_palette, 4); // Set the border white, using colour 0 from G_banner_palette // after converting the colours to hardware values cpct_setBorder(G_banner_palette[0]); // Infinite main loop while (1) { // Draw CPCtelera's Logo and wait for a while drawLogo(); for(i=0; i < WAITLOOPS; ++i); // Draw CPCtelera's Banner and wait for a while drawBanner(); for(i=0; i < WAITLOOPS; ++i); } }
///////////////////////////////////////////////////////////////// // initialize // Initialize this application when it starts. Disable firmware, // set the video mode and colours, and initialize map and cursor. // void initialize() { u8 *pmem; // Disable firmware to prevent it from restoring video mode or // interfering with our drawString functions cpct_disableFirmware(); // Set the video Mode to 1 (320x200, 4 colours) cpct_setVideoMode(1); // Use default colours except for palette index 0 (background). // Default colours are (Blue, Yellow, Cyan, Red), let's use // (Black, Yellow, Cyan, Red). Change only colour 0 and border. cpct_setPALColour(0, 0x14); cpct_setBorder(0x14); // Initialize Base Pointer of the map in video memory. This is // the place where the map will start to be drawn (0,0). This // location is (MAP_START_X, MAP_START_Y) with respect to CPCT_VMEM_START. pmem = cpct_getScreenPtr(CPCT_VMEM_START, MAP_START_X, MAP_START_Y); map_setBaseMem(pmem); // Set cursor at the top-left corner of the screen cursor_setLocation(0, 0); // Draw messages with instructions, the map and the cursor drawMessages(); map_draw(); cursor_draw(); }
////////////////////////////////////////////////////////////////////////////// // Initialization of the CPC // This function will be called once at the start of the program. After // that, it won't be required anymore. Therefore, its code will be placed at // 0x4000 to be used once and then removed when an image is written to // this second video buffer (0x4000-0x7FFF) /// void initializeCPC() { cpct_disableFirmware(); // Disable the firmware not to interfere with us cpct_setVideoMode(0); // Set mode 0 (160x200, 16 colours) cpct_setPalette(g_palette, 16); // Set colour palette cpct_setBorder(g_palette[0]); // Set the border with same colour used for background (0) // Set up the main video buffer (0xC000) with a message and all colours set up setUpVideoBuffer(VMEM_0, 0, "Main Screen Buffer", 6, 0); }
void initCPC() { cpct_disableFirmware(); cpct_fw2hw(g_palette,16); cpct_fw2hw(g_palette2,16); cpct_fw2hw(g_palette3,16); cpct_setPalette(g_palette,16); cpct_setBorder (g_palette[0]); cpct_setVideoMode(0); cpct_akp_musicInit(G_menu); // cpct_akp_SFXInit (G_menu); }
///////////////////////////////////////////////////////////////////////// // Initialization routine // Disables firmware, initializes palette and video mode and // draws the background // void initialization (){ cpct_disableFirmware(); // Disable firmware to prevent it from interfering cpct_setPalette(g_palette, 7); // Set palette using hardware colour values cpct_setBorder (g_palette[0]); // Set border colour same as background (0) cpct_setVideoMode(0); // Change to Mode 0 (160x200, 16 colours) // Set the internal tileset for drawing Tilemaps cpct_etm_setTileset2x4(g_tileset); // Draw the background tilemap cpct_etm_drawTilemap2x4_f(MAP_WIDTH_TILES, MAP_HEIGHT_TILES, CPCT_VMEM_START, g_background); }
// // EXAMPLE: Measuring free cycles after moving an sprite // void main(void) { u8 i; // Loop index u8 x=0, y=0; // Sprite coordinates (in bytes) u8* pvideomem = (u8*)0xC000; // Sprite initial video memory byte location (where it will be drawn) u16 avc = 0; // Available cycles until next VSYNC, after all main loop calculations // First, disable firmware to prevent it from intercepting our palette and video mode settings (and, // at the same time, winning some speed not having to process firmware code at every interrupt) cpct_disableFirmware(); // Set palette and Screen Border (transform firmware to hardware colours and then set them) cpct_fw2hw (G_palette, 4); cpct_setPalette(G_palette, 4); cpct_setBorder (G_palette[1]); // Ensure MODE 1 is set cpct_setVideoMode(1); // Main Loop while(1) { // First, wait VSYNC monitor signal to synchronize the loop with it. We'll start doing // calculations always at the same time (when VSYNC is first detected) cpct_waitVSYNC(); // Scan Keyboard and change sprite location if cursor keys are pressed cpct_scanKeyboard_f(); if (cpct_isKeyPressed(Key_CursorRight) && x < 80 - SPR_W) { x++; pvideomem++; } else if (cpct_isKeyPressed(Key_CursorLeft) && x > 0 ) { x--; pvideomem--; } if (cpct_isKeyPressed(Key_CursorUp) && y > 0 ) { pvideomem -= (y-- & 7) ? 0x0800 : 0xC850; } else if (cpct_isKeyPressed(Key_CursorDown) && y < 200 - SPR_H) { pvideomem += (++y & 7) ? 0x0800 : 0xC850; } // Draw the sprite at its new location on screen. // Sprite automatically erases previous copy of itself on the screen because it moves // 1 byte at a time and has a 0x00 border that overwrites previous colours on that place cpct_drawSprite(G_death, pvideomem, SPR_W, SPR_H); // Wait to next VSYNC signal, calculating the amount of free cycles (time we wait for VSYNC) // As documented on <cpct_count2VSYNC>, function returns number of loop iterations (L), and // cycles shall be calculated doing 22 + 34*L avc = 22 + 34 * cpct_count2VSYNC(); // Print 5 digits on the upper right corner of the screen, // with the amount of free cycles calculated in previous step. // Digits will be printed at screen locations (0xC046, 0xC048, 0xC04A, 0xC04C, 0xC04E) for(i=0; i<5; i++) { u8 digit = '0' + (avc % 10); cpct_drawCharM1_f((void*)(0xC04E - 2*i), 3, 0, digit); avc /= 10; } } }
// Initialization of the Amstrad CPC at the start of the application void initializeCPC() { // Disable firmware: we dont want it to interfere with our code cpct_disableFirmware(); // Set the function interruptHandler to be called on each interrupt. cpct_setInterruptHandler(interruptHandler); // Set the hardware palette (convert firmware colour values to hardware ones and set the palette) cpct_fw2hw(G_palette, 16); cpct_setPalette(G_palette, 16); // Descomentar estas tres lineas cuando tengamos paleta cpct_setBorder(G_palette[0]); // Change to Mode 0 (160x200, 16 colours) cpct_setVideoMode(0); }
// // MAIN: Using keyboard to move a sprite example // void main(void) { u8 x=10, y=10; // Sprite coordinates u8* pvideomem; // Pointer to video memory // // Set up the screen // // Disable firmware to prevent it from interfering with setPalette and setVideoMode cpct_disableFirmware(); // Set the colour palette cpct_fw2hw (G_palette, 4); // Convert our palette from firmware to hardware colours cpct_setPalette(G_palette, 4); // Set up the hardware palette using hardware colours // Set video mode 1 (320x200, 4 colours) cpct_setVideoMode(1); // // Infinite moving loop // while(1) { // Scan Keyboard (fastest routine) // The Keyboard has to be scanned to obtain pressed / not pressed status of // every key before checking each individual key's status. cpct_scanKeyboard_f(); // Check if user has pressed a Cursor Key and, if so, move the sprite if // it will still be inside screen boundaries if (cpct_isKeyPressed(Key_CursorRight) && x < (SCR_W - SP_W) ) ++x; else if (cpct_isKeyPressed(Key_CursorLeft) && x > 0 ) --x; if (cpct_isKeyPressed(Key_CursorUp) && y > 0 ) --y; else if (cpct_isKeyPressed(Key_CursorDown) && y < (SCR_H - SP_H) ) ++y; // Get video memory byte for coordinates x, y of the sprite (in bytes) pvideomem = cpct_getScreenPtr(CPCT_VMEM_START, x, y); // Draw the sprite in the video memory location got from coordinates x, y cpct_drawSprite(G_spriteLogoCT, pvideomem, SP_W, SP_H); } }
///////////////////////////////////////////////////////////////////////////////// // Main application's code // void application(void) { // Screen tilemap TScreenTilemap scr = { 0, 0, { 0, 0, MAP_WIDTH, MAP_HEIGHT} }; // First show user messages showMessages(); // Initialize the application cpct_disableFirmware(); // Firmware must be disabled for this application to work cpct_setBorder(0x00); // Set the border colour gray and.. cpct_setPALColour(0, 0x14); // ...background black // VERY IMPORTANT: Before using EasyTileMap functions (etm), the internal // pointer to the tileset must be set. cpct_etm_setTileset2x4(g_tileset); // Indefinitely draw the tilemap, listen to user input, // do changes and draw it again while(1) { drawScreenTilemap(&scr); // Redraws the tilemap readKeyboardInput(&scr); // Waits for a user input and makes associated changes } }
///////////////////////////////////////////////////////////////////////////////// // Machine initialization code // void initialize_CPC() { // Initialize the application cpct_disableFirmware(); // Firmware must be disabled for this application to work cpct_setVideoMode(0); // Set Mode 0 (160x200, 16 Colours) cpct_setPalette(g_palette, 13); // Set Palette cpct_setBorder(HW_BLACK); // Set the border and background colours to black // VERY IMPORTANT: Before using EasyTileMap functions (etm), the internal // pointer to the tileset must be set. cpct_etm_setTileset2x4(g_tileset); // Clean up the screen cpct_memset(CPCT_VMEM_START, 0x00, 0x4000); // Draw the full tilemap for the first time cpct_etm_drawTileBox2x4(0, 0, // (X, Y) upper-left corner of the tilemap SCR_TILE_WIDTH, MAP_HEIGHT, // (Width, Height) of the Box to be drawn (all the screen) MAP_WIDTH, // Width of the full tilemap (which is wider than the screen) CPCT_VMEM_START, // Pointer to the start of video memory (upper-left corner of the // ...tilemap in the screen) g_tilemap); // Pointer to the first tile of the tilemap to be drawn (upper-left // ... corner of the tilemap viewport window) }
// // MAIN LOOP // void main(void) { // Initialization cpct_disableFirmware(); cpct_setVideoMode(0); // Main loop: filling the screen using the 4 different basic // aligned functions in turns. while(1) { u8 i; // Loop counters u16 w; // 4 iterations of filling up the screen out of tiles using // the 4 different tile-drawing functions for (i=0; i < 6; i++) { // First, clear the screen and wait for a while cpct_clearScreen(0); for (w=0; w < WAITCLEARED; w++); // Then, fill up the screen with the next tile-drawing function and wait another while fillupScreen(&(tiles[i])); for (w=0; w < WAITPAINTED; w++); } } }
//////////////////////////////////////////////////////////////////////// // MAIN: Arkos Tracker Music Example // Keys: // * SPACE - Start / Stop Music // * 1 - Play a sound effect on Channel A // * 2 - Play a sound effect on Channel C // void main(void) { TKeyStatus k_space, k_0, k_1; // Status of the 3 Keys for this example (Space, 1, 2) u8 playing = 1; // Flag to know if music is playing or not u8 color = 1; // Color to draw charactes (normal / inverse) u8* pvideomem = CPCT_VMEM_START; // Pointer to video memory where next character will be drawn // All 3 keys are considered to be released at the start of the program k_space = k_0 = k_1 = K_RELEASED; // Initialize CPC cpct_disableFirmware(); // Disable firmware to prevent interaction cpct_setVideoMode(2); // Set Mode 2 (640x200, 2 colours) // Initialize the song to be played cpct_akp_musicInit(molusk_song); // Initialize the music cpct_akp_SFXInit(molusk_song); // Initialize instruments to be used for SFX (Same as music song) while (1) { // We have to call the play function 50 times per second (because the song is // designed at 50Hz). We only have to wait for VSYNC and call the play function // when the song is not stopped (still playing) cpct_waitVSYNC(); // Check if the music is playing. When it is, do all the things the music // requires to be done every 1/50 secs. if (playing) { cpct_akp_musicPlay(); // Play next music 1/50 step. // Write a new number to the screen to see something while playing. // The number will be 0 when music is playing, and 1 when it finishes. // -> If some SFX is playing write the channel where it is playing // Check if there is an instrument plaing on channel A if (cpct_akp_SFXGetInstrument(AY_CHANNEL_A)) cpct_drawCharM2(pvideomem, color, 'A'); // Write an 'A' because channel A is playing // Check if there is an instrument plaing on channel C else if (cpct_akp_SFXGetInstrument(AY_CHANNEL_C)) cpct_drawCharM2(pvideomem, color, 'C'); // Write an 'C' because channel A is playing // No SFX is playing on Channels A or C, write the number of times // this song has looped. else cpct_drawCharM2(pvideomem, color, '0' + cpct_akp_songLoopTimes); // Point to the start of the next character in video memory if (++pvideomem >= (u8*)0xC7D0) { pvideomem = CPCT_VMEM_START; // When we reach the end of the screen, we return.. color ^= 1; // .. to the start, and change the colour } // Check if music has already ended (when looptimes is > 0) if (cpct_akp_songLoopTimes > 0) cpct_akp_musicInit(molusk_song); // Song has ended, start it again and set loop to 0 } // Check keyboard to let the user play/stop the song with de Space Bar // and reproduce some sound effects with keys 1 and 0 cpct_scanKeyboard_f(); // When Space is released, stop / continue music if ( checkKeyEvent(Key_Space, &k_space) == K_RELEASED ) { // Only stop it when it was playing previously // No need to call "play" again when continuing, as the // change in "playing" status will make the program call "play" // again from the next cycle on if (playing) cpct_akp_stop(); // Change it from playing to not playing and viceversa (0 to 1, 1 to 0) playing ^= 1; // Check if Key 0 has been released to reproduce a Sound effect on channel A } else if ( checkKeyEvent(Key_0, &k_0) == K_RELEASED ) { cpct_akp_SFXPlay(13, 15, 36, 20, 0, AY_CHANNEL_A); // Check if Key 1 has been released to reproduce a Sound effect on channel C } else if ( checkKeyEvent(Key_1, &k_1) == K_RELEASED ) cpct_akp_SFXPlay(3, 15, 60, 0, 40, AY_CHANNEL_C); } }
// // Bit Arrays Example: Main // void main (void) { u8 i, j; // Counters for loops u8 array1[10]; // Array of 10 bytes, 80 bits, to be used bit by bit u8 array2[20]; // Array of 20 bytes, 160 bits, 80 groups of 2 bits. u8 array4[40]; // Array of 40 bytes, 320 bits, 80 groups of 4 bits. // Disable firmware to prevent it from restoring video mode or // interfering with our drawChar functions cpct_disableFirmware(); // Set mode 2 for visual clarity on arrays printed cpct_setVideoMode(2); // // Main Loop: loop forever showing arrays // while(1) { // First, erase all contents of our 3 arrays, // setting all their bits to 0 cpct_memset(array1, 0, 10); cpct_memset(array2, 0, 20); cpct_memset(array4, 0, 40); // // Test 1: Set to 1 each bit on the array1 individually (all others to 0) // for (i = 0; i < 80; ++i) { // Set Bit i to 1 cpct_setBit(array1, i, 1); // Print the complete array at the top of the screen printArray((u8*)0xC000, array1, 80, f_getbit); // Reset again the bit to 0 an iterate cpct_setBit(array1, i, 0); } // // Test 2: Fill in the array2 with individual values from 3 to 1 // (all the rest should be 0) // for (j = 3; j > 0; --j) { for (i = 0; i < 80; ++i) { // Set the index i to the value j (1 to 3) cpct_set2Bits(array2, i, j); // Print the complete array printArray((u8*)0xC0A0, array2, 80, f_get2bits); // Reset the value of the item to 0 again cpct_set2Bits(array2, i, 0); } } // // Test 3: Fill in the array4 with consecutive elements from 0 to 15, // 16 times, rotating all the 16 elements through all the positions // in the array. // for (j = 0; j < 16; j++) { for (i = 0; i < 80; ++i) { // Increment value using loop indexes and calculate module 16 (AND 0x0F) u8 value = (i + j) & 0x0F; // Set next 4-bits element (i) to the calculated value and print the array cpct_set4Bits(array4, i, value); printArray((u8*)0xC140, array4, 80, f_get4bits); } } // // Test 4: Fill the array1 with 1's // for (i = 0; i < 80; ++i) { // Set next bit i to 1 cpct_setBit(array1, i, 1); // Print the complete array1 again printArray((u8*)0xC000, array1, 80, f_getbit); } // // Test 5: Fill the array2 with 3's, then with 2's and then with 1's // for (j = 3; j > 0; --j) { for (i = 0; i < 80; ++i) { // Set next bit i to j (3, 2, 1) cpct_set2Bits(array2, i, j); // Print the complete array again printArray((u8*)0xC0A0, array2, 80, f_get2bits); } } } }