diff --git a/.gitignore b/.gitignore index 3d19507..f4d00fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.o apple2e +*.gif diff --git a/apple2e.cpp b/apple2e.cpp index 1325921..85e6bbe 100644 --- a/apple2e.cpp +++ b/apple2e.cpp @@ -41,6 +41,9 @@ volatile bool exit_on_memory_fallthrough = true; volatile bool run_fast = false; volatile bool pause_cpu = false; +bool run_rate_limited = false; +int rate_limit_millis; + // XXX - this should be handled through a function passed to MAINboard APPLE2Einterface::ModeHistory mode_history; @@ -3069,8 +3072,14 @@ enum APPLE2Einterface::EventType process_events(MAINboard *board, bus_frontend& pause_cpu = e.value; } else if(e.type == APPLE2Einterface::SPEED) { run_fast = e.value; - } else if(e.type == APPLE2Einterface::QUIT) - return e.type; + } else if(e.type == APPLE2Einterface::QUIT) { + return e.type; + } else if(e.type == APPLE2Einterface::REQUEST_ITERATION_PERIOD_IN_MILLIS) { + run_rate_limited = true; + rate_limit_millis = e.value; + } else if(e.type == APPLE2Einterface::WITHDRAW_ITERATION_PERIOD_REQUEST) { + run_rate_limited = false; + } } return APPLE2Einterface::NONE; } @@ -3231,10 +3240,13 @@ int main(int argc, char **argv) if(pause_cpu) clocks_per_slice = 0; else { - if(run_fast) + if(run_rate_limited) { + clocks_per_slice = machine_clock_rate / 1000 * rate_limit_millis; + } else if(run_fast) { clocks_per_slice = machine_clock_rate / 5; - else + } else { clocks_per_slice = millis_per_slice * machine_clock_rate / 1000 * 1.05; + } } clk_t prev_clock = clk; while(clk - prev_clock < clocks_per_slice) { @@ -3259,7 +3271,7 @@ int main(int argc, char **argv) auto elapsed_millis = chrono::duration_cast(now - then); if(!run_fast || pause_cpu) - this_thread::sleep_for(chrono::milliseconds(millis_per_slice) - elapsed_millis); + this_thread::sleep_for(chrono::milliseconds(clocks_per_slice * 1000 / machine_clock_rate) - elapsed_millis); then = now; diff --git a/gif.h b/gif.h new file mode 100644 index 0000000..e5dbb62 --- /dev/null +++ b/gif.h @@ -0,0 +1,825 @@ +// +// gif.h +// by Charlie Tangora +// Public domain. +// Email me : ctangora -at- gmail -dot- com +// +// This file offers a simple, very limited way to create animated GIFs directly in code. +// +// Those looking for particular cleverness are likely to be disappointed; it's pretty +// much a straight-ahead implementation of the GIF format with optional Floyd-Steinberg +// dithering. (It does at least use delta encoding - only the changed portions of each +// frame are saved.) +// +// So resulting files are often quite large. The hope is that it will be handy nonetheless +// as a quick and easily-integrated way for programs to spit out animations. +// +// Only RGBA8 is currently supported as an input format. (The alpha is ignored.) +// +// USAGE: +// Create a GifWriter struct. Pass it to GifBegin() to initialize and write the header. +// Pass subsequent frames to GifWriteFrame(). +// Finally, call GifEnd() to close the file handle and free memory. +// + +#ifndef gif_h +#define gif_h + +#include // for FILE* +#include // for memcpy and bzero +#include // for integer typedefs + +// Define these macros to hook into a custom memory allocator. +// TEMP_MALLOC and TEMP_FREE will only be called in stack fashion - frees in the reverse order of mallocs +// and any temp memory allocated by a function will be freed before it exits. +// MALLOC and FREE are used only by GifBegin and GifEnd respectively (to allocate a buffer the size of the image, which +// is used to find changed pixels for delta-encoding.) + +#ifndef GIF_TEMP_MALLOC +#include +#define GIF_TEMP_MALLOC malloc +#endif + +#ifndef GIF_TEMP_FREE +#include +#define GIF_TEMP_FREE free +#endif + +#ifndef GIF_MALLOC +#include +#define GIF_MALLOC malloc +#endif + +#ifndef GIF_FREE +#include +#define GIF_FREE free +#endif + +const int kGifTransIndex = 0; + +struct GifPalette +{ + int bitDepth; + + uint8_t r[256]; + uint8_t g[256]; + uint8_t b[256]; + + // k-d tree over RGB space, organized in heap fashion + // i.e. left child of node i is node i*2, right child is node i*2+1 + // nodes 256-511 are implicitly the leaves, containing a color + uint8_t treeSplitElt[255]; + uint8_t treeSplit[255]; +}; + +// max, min, and abs functions +int GifIMax(int l, int r) { return l>r?l:r; } +int GifIMin(int l, int r) { return l (1<bitDepth)-1) + { + int ind = treeRoot-(1<bitDepth); + if(ind == kGifTransIndex) return; + + // check whether this color is better than the current winner + int r_err = r - ((int32_t)pPal->r[ind]); + int g_err = g - ((int32_t)pPal->g[ind]); + int b_err = b - ((int32_t)pPal->b[ind]); + int diff = GifIAbs(r_err)+GifIAbs(g_err)+GifIAbs(b_err); + + if(diff < bestDiff) + { + bestInd = ind; + bestDiff = diff; + } + + return; + } + + // take the appropriate color (r, g, or b) for this node of the k-d tree + int comps[3]; comps[0] = r; comps[1] = g; comps[2] = b; + int splitComp = comps[pPal->treeSplitElt[treeRoot]]; + + int splitPos = pPal->treeSplit[treeRoot]; + if(splitPos > splitComp) + { + // check the left subtree + GifGetClosestPaletteColor(pPal, r, g, b, bestInd, bestDiff, treeRoot*2); + if( bestDiff > splitPos - splitComp ) + { + // cannot prove there's not a better value in the right subtree, check that too + GifGetClosestPaletteColor(pPal, r, g, b, bestInd, bestDiff, treeRoot*2+1); + } + } + else + { + GifGetClosestPaletteColor(pPal, r, g, b, bestInd, bestDiff, treeRoot*2+1); + if( bestDiff > splitComp - splitPos ) + { + GifGetClosestPaletteColor(pPal, r, g, b, bestInd, bestDiff, treeRoot*2); + } + } +} + +void GifSwapPixels(uint8_t* image, int pixA, int pixB) +{ + uint8_t rA = image[pixA*4]; + uint8_t gA = image[pixA*4+1]; + uint8_t bA = image[pixA*4+2]; + uint8_t aA = image[pixA*4+3]; + + uint8_t rB = image[pixB*4]; + uint8_t gB = image[pixB*4+1]; + uint8_t bB = image[pixB*4+2]; + uint8_t aB = image[pixA*4+3]; + + image[pixA*4] = rB; + image[pixA*4+1] = gB; + image[pixA*4+2] = bB; + image[pixA*4+3] = aB; + + image[pixB*4] = rA; + image[pixB*4+1] = gA; + image[pixB*4+2] = bA; + image[pixB*4+3] = aA; +} + +// just the partition operation from quicksort +int GifPartition(uint8_t* image, const int left, const int right, const int elt, int pivotIndex) +{ + const int pivotValue = image[(pivotIndex)*4+elt]; + GifSwapPixels(image, pivotIndex, right-1); + int storeIndex = left; + bool split = 0; + for(int ii=left; ii neededCenter) + GifPartitionByMedian(image, left, pivotIndex, com, neededCenter); + + if(pivotIndex < neededCenter) + GifPartitionByMedian(image, pivotIndex+1, right, com, neededCenter); + } +} + +// Builds a palette by creating a balanced k-d tree of all pixels in the image +void GifSplitPalette(uint8_t* image, int numPixels, int firstElt, int lastElt, int splitElt, int splitDist, int treeNode, bool buildForDither, GifPalette* pal) +{ + if(lastElt <= firstElt || numPixels == 0) + return; + + // base case, bottom of the tree + if(lastElt == firstElt+1) + { + if(buildForDither) + { + // Dithering needs at least one color as dark as anything + // in the image and at least one brightest color - + // otherwise it builds up error and produces strange artifacts + if( firstElt == 1 ) + { + // special case: the darkest color in the image + uint32_t r=255, g=255, b=255; + for(int ii=0; iir[firstElt] = (uint8_t)r; + pal->g[firstElt] = (uint8_t)g; + pal->b[firstElt] = (uint8_t)b; + + return; + } + + if( firstElt == (1 << pal->bitDepth)-1 ) + { + // special case: the lightest color in the image + uint32_t r=0, g=0, b=0; + for(int ii=0; iir[firstElt] = (uint8_t)r; + pal->g[firstElt] = (uint8_t)g; + pal->b[firstElt] = (uint8_t)b; + + return; + } + } + + // otherwise, take the average of all colors in this subcube + uint64_t r=0, g=0, b=0; + for(int ii=0; iir[firstElt] = (uint8_t)r; + pal->g[firstElt] = (uint8_t)g; + pal->b[firstElt] = (uint8_t)b; + + return; + } + + // Find the axis with the largest range + int minR = 255, maxR = 0; + int minG = 255, maxG = 0; + int minB = 255, maxB = 0; + for(int ii=0; ii maxR) maxR = r; + if(r < minR) minR = r; + + if(g > maxG) maxG = g; + if(g < minG) minG = g; + + if(b > maxB) maxB = b; + if(b < minB) minB = b; + } + + int rRange = maxR - minR; + int gRange = maxG - minG; + int bRange = maxB - minB; + + // and split along that axis. (incidentally, this means this isn't a "proper" k-d tree but I don't know what else to call it) + int splitCom = 1; + if(bRange > gRange) splitCom = 2; + if(rRange > bRange && rRange > gRange) splitCom = 0; + + int subPixelsA = numPixels * (splitElt - firstElt) / (lastElt - firstElt); + int subPixelsB = numPixels-subPixelsA; + + GifPartitionByMedian(image, 0, numPixels, splitCom, subPixelsA); + + pal->treeSplitElt[treeNode] = (uint8_t)splitCom; + pal->treeSplit[treeNode] = image[subPixelsA*4+splitCom]; + + GifSplitPalette(image, subPixelsA, firstElt, splitElt, splitElt-splitDist, splitDist/2, treeNode*2, buildForDither, pal); + GifSplitPalette(image+subPixelsA*4, subPixelsB, splitElt, lastElt, splitElt+splitDist, splitDist/2, treeNode*2+1, buildForDither, pal); +} + +// Finds all pixels that have changed from the previous image and +// moves them to the fromt of th buffer. +// This allows us to build a palette optimized for the colors of the +// changed pixels only. +int GifPickChangedPixels( const uint8_t* lastFrame, uint8_t* frame, int numPixels ) +{ + int numChanged = 0; + uint8_t* writeIter = frame; + + for (int ii=0; iibitDepth = bitDepth; + + // SplitPalette is destructive (it sorts the pixels by color) so + // we must create a copy of the image for it to destroy + size_t imageSize = (size_t)(width * height * 4 * sizeof(uint8_t)); + uint8_t* destroyableImage = (uint8_t*)GIF_TEMP_MALLOC(imageSize); + memcpy(destroyableImage, nextFrame, imageSize); + + int numPixels = (int)(width * height); + if(lastFrame) + numPixels = GifPickChangedPixels(lastFrame, destroyableImage, numPixels); + + const int lastElt = 1 << bitDepth; + const int splitElt = lastElt/2; + const int splitDist = splitElt/2; + + GifSplitPalette(destroyableImage, numPixels, 1, lastElt, splitElt, splitDist, 1, buildForDither, pPal); + + GIF_TEMP_FREE(destroyableImage); + + // add the bottom node for the transparency index + pPal->treeSplit[1 << (bitDepth-1)] = 0; + pPal->treeSplitElt[1 << (bitDepth-1)] = 0; + + pPal->r[0] = pPal->g[0] = pPal->b[0] = 0; +} + +// Implements Floyd-Steinberg dithering, writes palette value to alpha +void GifDitherImage( const uint8_t* lastFrame, const uint8_t* nextFrame, uint8_t* outFrame, uint32_t width, uint32_t height, GifPalette* pPal ) +{ + int numPixels = (int)(width * height); + + // quantPixels initially holds color*256 for all pixels + // The extra 8 bits of precision allow for sub-single-color error values + // to be propagated + int32_t *quantPixels = (int32_t *)GIF_TEMP_MALLOC(sizeof(int32_t) * (size_t)numPixels * 4); + + for( int ii=0; iir[bestInd]) * 256; + int32_t g_err = nextPix[1] - int32_t(pPal->g[bestInd]) * 256; + int32_t b_err = nextPix[2] - int32_t(pPal->b[bestInd]) * 256; + + nextPix[0] = pPal->r[bestInd]; + nextPix[1] = pPal->g[bestInd]; + nextPix[2] = pPal->b[bestInd]; + nextPix[3] = bestInd; + + // Propagate the error to the four adjacent locations + // that we haven't touched yet + int quantloc_7 = (int)(yy * width + xx + 1); + int quantloc_3 = (int)(yy * width + width + xx - 1); + int quantloc_5 = (int)(yy * width + width + xx); + int quantloc_1 = (int)(yy * width + width + xx + 1); + + if(quantloc_7 < numPixels) + { + int32_t* pix7 = quantPixels+4*quantloc_7; + pix7[0] += GifIMax( -pix7[0], r_err * 7 / 16 ); + pix7[1] += GifIMax( -pix7[1], g_err * 7 / 16 ); + pix7[2] += GifIMax( -pix7[2], b_err * 7 / 16 ); + } + + if(quantloc_3 < numPixels) + { + int32_t* pix3 = quantPixels+4*quantloc_3; + pix3[0] += GifIMax( -pix3[0], r_err * 3 / 16 ); + pix3[1] += GifIMax( -pix3[1], g_err * 3 / 16 ); + pix3[2] += GifIMax( -pix3[2], b_err * 3 / 16 ); + } + + if(quantloc_5 < numPixels) + { + int32_t* pix5 = quantPixels+4*quantloc_5; + pix5[0] += GifIMax( -pix5[0], r_err * 5 / 16 ); + pix5[1] += GifIMax( -pix5[1], g_err * 5 / 16 ); + pix5[2] += GifIMax( -pix5[2], b_err * 5 / 16 ); + } + + if(quantloc_1 < numPixels) + { + int32_t* pix1 = quantPixels+4*quantloc_1; + pix1[0] += GifIMax( -pix1[0], r_err / 16 ); + pix1[1] += GifIMax( -pix1[1], g_err / 16 ); + pix1[2] += GifIMax( -pix1[2], b_err / 16 ); + } + } + } + + // Copy the palettized result to the output buffer + for( int ii=0; iir[bestInd]; + outFrame[1] = pPal->g[bestInd]; + outFrame[2] = pPal->b[bestInd]; + outFrame[3] = (uint8_t)bestInd; + } + + if(lastFrame) lastFrame += 4; + outFrame += 4; + nextFrame += 4; + } +} + +// Simple structure to write out the LZW-compressed portion of the image +// one bit at a time +struct GifBitStatus +{ + uint8_t bitIndex; // how many bits in the partial byte written so far + uint8_t byte; // current partial byte + + uint32_t chunkIndex; + uint8_t chunk[256]; // bytes are written in here until we have 256 of them, then written to the file +}; + +// insert a single bit +void GifWriteBit( GifBitStatus& stat, uint32_t bit ) +{ + bit = bit & 1; + bit = bit << stat.bitIndex; + stat.byte |= bit; + + ++stat.bitIndex; + if( stat.bitIndex > 7 ) + { + // move the newly-finished byte to the chunk buffer + stat.chunk[stat.chunkIndex++] = stat.byte; + // and start a new byte + stat.bitIndex = 0; + stat.byte = 0; + } +} + +// write all bytes so far to the file +void GifWriteChunk( FILE* f, GifBitStatus& stat ) +{ + fputc((int)stat.chunkIndex, f); + fwrite(stat.chunk, 1, stat.chunkIndex, f); + + stat.bitIndex = 0; + stat.byte = 0; + stat.chunkIndex = 0; +} + +void GifWriteCode( FILE* f, GifBitStatus& stat, uint32_t code, uint32_t length ) +{ + for( uint32_t ii=0; ii> 1; + + if( stat.chunkIndex == 255 ) + { + GifWriteChunk(f, stat); + } + } +} + +// The LZW dictionary is a 256-ary tree constructed as the file is encoded, +// this is one node +struct GifLzwNode +{ + uint16_t m_next[256]; +}; + +// write a 256-color (8-bit) image palette to the file +void GifWritePalette( const GifPalette* pPal, FILE* f ) +{ + fputc(0, f); // first color: transparency + fputc(0, f); + fputc(0, f); + + for(int ii=1; ii<(1 << pPal->bitDepth); ++ii) + { + uint32_t r = pPal->r[ii]; + uint32_t g = pPal->g[ii]; + uint32_t b = pPal->b[ii]; + + fputc((int)r, f); + fputc((int)g, f); + fputc((int)b, f); + } +} + +// write the image header, LZW-compress and write out the image +void GifWriteLzwImage(FILE* f, uint8_t* image, uint32_t left, uint32_t top, uint32_t width, uint32_t height, uint32_t delay, GifPalette* pPal) +{ + // graphics control extension + fputc(0x21, f); + fputc(0xf9, f); + fputc(0x04, f); + fputc(0x05, f); // leave prev frame in place, this frame has transparency + fputc(delay & 0xff, f); + fputc((delay >> 8) & 0xff, f); + fputc(kGifTransIndex, f); // transparent color index + fputc(0, f); + + fputc(0x2c, f); // image descriptor block + + fputc(left & 0xff, f); // corner of image in canvas space + fputc((left >> 8) & 0xff, f); + fputc(top & 0xff, f); + fputc((top >> 8) & 0xff, f); + + fputc(width & 0xff, f); // width and height of image + fputc((width >> 8) & 0xff, f); + fputc(height & 0xff, f); + fputc((height >> 8) & 0xff, f); + + //fputc(0, f); // no local color table, no transparency + //fputc(0x80, f); // no local color table, but transparency + + fputc(0x80 + pPal->bitDepth-1, f); // local color table present, 2 ^ bitDepth entries + GifWritePalette(pPal, f); + + const int minCodeSize = pPal->bitDepth; + const uint32_t clearCode = 1 << pPal->bitDepth; + + fputc(minCodeSize, f); // min code size 8 bits + + GifLzwNode* codetree = (GifLzwNode*)GIF_TEMP_MALLOC(sizeof(GifLzwNode)*4096); + + memset(codetree, 0, sizeof(GifLzwNode)*4096); + int32_t curCode = -1; + uint32_t codeSize = (uint32_t)minCodeSize + 1; + uint32_t maxCode = clearCode+1; + + GifBitStatus stat; + stat.byte = 0; + stat.bitIndex = 0; + stat.chunkIndex = 0; + + GifWriteCode(f, stat, clearCode, codeSize); // start with a fresh LZW dictionary + + for(uint32_t yy=0; yy= (1ul << codeSize) ) + { + // dictionary entry count has broken a size barrier, + // we need more bits for codes + codeSize++; + } + if( maxCode == 4095 ) + { + // the dictionary is full, clear it out and begin anew + GifWriteCode(f, stat, clearCode, codeSize); // clear tree + + memset(codetree, 0, sizeof(GifLzwNode)*4096); + codeSize = (uint32_t)(minCodeSize + 1); + maxCode = clearCode+1; + } + + curCode = nextValue; + } + } + } + + // compression footer + GifWriteCode(f, stat, (uint32_t)curCode, codeSize); + GifWriteCode(f, stat, clearCode, codeSize); + GifWriteCode(f, stat, clearCode + 1, (uint32_t)minCodeSize + 1); + + // write out the last partial chunk + while( stat.bitIndex ) GifWriteBit(stat, 0); + if( stat.chunkIndex ) GifWriteChunk(f, stat); + + fputc(0, f); // image block terminator + + GIF_TEMP_FREE(codetree); +} + +struct GifWriter +{ + FILE* f; + uint8_t* oldImage; + bool firstFrame; +}; + +// Creates a gif file. +// The input GIFWriter is assumed to be uninitialized. +// The delay value is the time between frames in hundredths of a second - note that not all viewers pay much attention to this value. +bool GifBegin( GifWriter* writer, const char* filename, uint32_t width, uint32_t height, uint32_t delay, int32_t bitDepth = 8, bool dither = false ) +{ + (void)bitDepth; (void)dither; // Mute "Unused argument" warnings +#if defined(_MSC_VER) && (_MSC_VER >= 1400) + writer->f = 0; + fopen_s(&writer->f, filename, "wb"); +#else + writer->f = fopen(filename, "wb"); +#endif + if(!writer->f) return false; + + writer->firstFrame = true; + + // allocate + writer->oldImage = (uint8_t*)GIF_MALLOC(width*height*4); + + fputs("GIF89a", writer->f); + + // screen descriptor + fputc(width & 0xff, writer->f); + fputc((width >> 8) & 0xff, writer->f); + fputc(height & 0xff, writer->f); + fputc((height >> 8) & 0xff, writer->f); + + fputc(0xf0, writer->f); // there is an unsorted global color table of 2 entries + fputc(0, writer->f); // background color + fputc(0, writer->f); // pixels are square (we need to specify this because it's 1989) + + // now the "global" palette (really just a dummy palette) + // color 0: black + fputc(0, writer->f); + fputc(0, writer->f); + fputc(0, writer->f); + // color 1: also black + fputc(0, writer->f); + fputc(0, writer->f); + fputc(0, writer->f); + + if( delay != 0 ) + { + // animation header + fputc(0x21, writer->f); // extension + fputc(0xff, writer->f); // application specific + fputc(11, writer->f); // length 11 + fputs("NETSCAPE2.0", writer->f); // yes, really + fputc(3, writer->f); // 3 bytes of NETSCAPE2.0 data + + fputc(1, writer->f); // JUST BECAUSE + fputc(0, writer->f); // loop infinitely (byte 0) + fputc(0, writer->f); // loop infinitely (byte 1) + + fputc(0, writer->f); // block terminator + } + + return true; +} + +// Writes out a new frame to a GIF in progress. +// The GIFWriter should have been created by GIFBegin. +// AFAIK, it is legal to use different bit depths for different frames of an image - +// this may be handy to save bits in animations that don't change much. +bool GifWriteFrame( GifWriter* writer, const uint8_t* image, uint32_t width, uint32_t height, uint32_t delay, int bitDepth = 8, bool dither = false ) +{ + if(!writer->f) return false; + + const uint8_t* oldImage = writer->firstFrame? NULL : writer->oldImage; + writer->firstFrame = false; + + GifPalette pal; + GifMakePalette((dither? NULL : oldImage), image, width, height, bitDepth, dither, &pal); + + if(dither) + GifDitherImage(oldImage, image, writer->oldImage, width, height, &pal); + else + GifThresholdImage(oldImage, image, writer->oldImage, width, height, &pal); + + GifWriteLzwImage(writer->f, writer->oldImage, 0, 0, width, height, delay, &pal); + + return true; +} + +// Writes the EOF code, closes the file handle, and frees temp memory used by a GIF. +// Many if not most viewers will still display a GIF properly if the EOF code is missing, +// but it's still a good idea to write it out. +bool GifEnd( GifWriter* writer ) +{ + if(!writer->f) return false; + + fputc(0x3b, writer->f); // end of file + fclose(writer->f); + GIF_FREE(writer->oldImage); + + writer->f = NULL; + writer->oldImage = NULL; + + return true; +} + +#endif diff --git a/interface.cpp b/interface.cpp index 7d4d7b6..2a61fd1 100644 --- a/interface.cpp +++ b/interface.cpp @@ -13,6 +13,8 @@ #include #include +#include "gif.h" + // implicit centering in widget? Or special centering widget? // lines (for around toggle and momentary) // widget which is graphics/text/lores screen @@ -80,6 +82,105 @@ struct vertex_array : public vector } }; +/* + * OpenGL Render Target ; creates a framebuffer that can be used as a + * rendering target and as a texture color source. + */ +struct render_target +{ + GLuint framebuffer; + GLuint color; + GLuint depth; + + render_target(int w, int h); + ~render_target(); + + // Start rendering; Draw()s will draw to this framebuffer + void start_rendering() + { + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); + } + + // Stop rendering; Draw()s will draw to the back buffer + void stop_rendering() + { + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + } + + // Start reading; Read()s will read from this framebuffer + void start_reading() + { + glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer); + glReadBuffer(GL_COLOR_ATTACHMENT0); + } + + // Stop reading; Read()s will read from the back buffer + void stop_reading() + { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + glReadBuffer(GL_BACK); + } + + // Use this color as the currently bound texture source + void use_color() + { + glBindTexture(GL_TEXTURE_2D, color); + } +}; + +// Destroy render target resources +render_target::~render_target() +{ + glDeleteTextures(1, &color); + glDeleteRenderbuffers(1, &depth); + glDeleteFramebuffers(1, &framebuffer); +} + +// Create render target resources if possible +render_target::render_target(int w, int h) +{ + GLenum status; + + // Create color texture + glGenTextures(1, &color); + glBindTexture(GL_TEXTURE_2D, color); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + + CheckOpenGL(__FILE__, __LINE__); + + // Create depth texture + glGenTextures(1, &depth); + glBindTexture(GL_TEXTURE_2D, depth); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, w, h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); + CheckOpenGL(__FILE__, __LINE__); + + glGenFramebuffers(1, &framebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + CheckOpenGL(__FILE__, __LINE__); + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0); + CheckOpenGL(__FILE__, __LINE__); + + status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if(status != GL_FRAMEBUFFER_COMPLETE) { + fprintf(stderr, "framebuffer status was %04X\n", status); + throw "Couldn't create OpenGL framebuffer"; + } + CheckOpenGL(__FILE__, __LINE__); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +const int apple2_screen_width = 280; +const int apple2_screen_height = 192; +const int recording_scale = 2; +const int recording_frame_duration_hundredths = 5; + chrono::time_point start_time; static GLFWwindow* my_window; @@ -109,9 +210,11 @@ deque event_queue; bool force_caps_on = true; bool draw_using_color = false; -ModeSettings line_to_mode[192]; +ModeSettings line_to_mode[apple2_screen_height]; ModePoint most_recent_modepoint; -vertex_array line_to_area[192]; +vertex_array line_to_area[apple2_screen_height]; + +render_target *rendertarget_for_recording; bool event_waiting() { @@ -643,8 +746,8 @@ vertex_array make_rectangle_vertex_array(float x, float y, float w, float h) void initialize_screen_areas() { - for(int i = 0; i < 192; i++) { - line_to_area[i] = make_rectangle_vertex_array(0, i, 280, 1); + for(int i = 0; i < apple2_screen_height; i++) { + line_to_area[i] = make_rectangle_vertex_array(0, i, apple2_screen_width, 1); } } @@ -1051,7 +1154,7 @@ struct apple2screen : public widget virtual width_height get_min_dimensions() const { - return {280, 192}; + return {apple2_screen_width, apple2_screen_height}; } virtual void draw(double now, float to_screen[9], float x, float y, float w_, float h_) @@ -1060,7 +1163,7 @@ struct apple2screen : public widget h = h_; long long elapsed_millis = now * 1000; - for(int i = 0; i < 192; i++) { + for(int i = 0; i < apple2_screen_height; i++) { const ModeSettings& settings = line_to_mode[i]; set_shader(to_screen, settings.mode, (i < 160) ? false : settings.mixed, settings.page, settings.vid80, (elapsed_millis / 300) % 2, x, y); @@ -1379,10 +1482,29 @@ struct toggle : public text_widget } on = !on; } + + /** + * Sets the boolean value, updates the UI, and calls the appropriate callback. + */ + void set_value(bool value) { + on = value; + + if(on) { + set(fg, 0, 0, 0, 1); + set(bg, 1, 1, 1, 1); + action_on(); + } else { + set(fg, 1, 1, 1, 1); + set(bg, 0, 0, 0, 1); + action_off(); + } + } }; widget *ui; +widget *screen_only; toggle *caps_toggle; +toggle *record_toggle; void initialize_gl(void) { @@ -1620,6 +1742,40 @@ struct floppy_icon : public widget } }; +// Globals for GIF recording. +static GifWriter gif_writer; +static bool gif_recording = false; + +/** + * Stop recording all frames to a GIF file. + */ +static void stop_record() +{ + if (gif_recording) { + GifEnd(&gif_writer); + gif_recording = false; + event_queue.push_back({WITHDRAW_ITERATION_PERIOD_REQUEST, 0}); + } +} + +/** + * Start recording all frames to a GIF file. + */ +static void start_record() +{ + if (gif_recording) { + stop_record(); + } + + if(!rendertarget_for_recording) { + rendertarget_for_recording = new render_target(apple2_screen_width * recording_scale, apple2_screen_height * recording_scale); + } + + GifBegin(&gif_writer, "out.gif", apple2_screen_width * recording_scale, apple2_screen_height * recording_scale, recording_frame_duration_hundredths); + event_queue.push_back({REQUEST_ITERATION_PERIOD_IN_MILLIS, recording_frame_duration_hundredths * 10}); + gif_recording = true; +} + floppy_icon *floppy0_icon; floppy_icon *floppy1_icon; @@ -1632,8 +1788,9 @@ void initialize_widgets(bool run_fast, bool add_floppies, bool floppy0_inserted, caps_toggle = new toggle("CAPS", true, [](){force_caps_on = true;}, [](){force_caps_on = false;}); toggle *color_toggle = new toggle("COLOR", false, [](){draw_using_color = true;}, [](){draw_using_color = false;}); toggle *pause_toggle = new toggle("PAUSE", false, [](){event_queue.push_back({PAUSE, 1});}, [](){event_queue.push_back({PAUSE, 0});}); + record_toggle = new toggle("RECORD", false, [](){start_record();}, [](){stop_record();}); - vector controls = {reset_momentary, reboot_momentary, fast_toggle, caps_toggle, color_toggle, pause_toggle}; + vector controls = {reset_momentary, reboot_momentary, fast_toggle, caps_toggle, color_toggle, pause_toggle, record_toggle}; if(add_floppies) { floppy0_icon = new floppy_icon(0, floppy0_inserted); floppy1_icon = new floppy_icon(1, floppy1_inserted); @@ -1644,9 +1801,9 @@ void initialize_widgets(bool run_fast, bool add_floppies, bool floppy0_inserted, for(auto b : controls) controls_centered.push_back(new centering(b)); - widget *screen = new apple2screen(); + screen_only = new apple2screen(); widget *buttonpanel = new centering(new widgetbox(widgetbox::VERTICAL, controls_centered)); - vector panels_centered = {new spacer(10, 0), new centering(screen), new spacer(10, 0), new centering(buttonpanel), new spacer(10, 0)}; + vector panels_centered = {new spacer(10, 0), new centering(screen_only), new spacer(10, 0), new centering(buttonpanel), new spacer(10, 0)}; ui = new centering(new widgetbox(widgetbox::HORIZONTAL, panels_centered)); } @@ -1663,6 +1820,7 @@ void show_floppy_activity(int number, bool activity) float pixel_to_ui_scale; float to_screen_transform[9]; +float recording_transform[9]; void make_to_screen_transform() { @@ -1675,6 +1833,16 @@ void make_to_screen_transform() to_screen_transform[2 * 3 + 0] = -1; to_screen_transform[2 * 3 + 1] = 1; to_screen_transform[2 * 3 + 2] = 1; + + recording_transform[0 * 3 + 0] = 2.0 / apple2_screen_width; + recording_transform[0 * 3 + 1] = 0; + recording_transform[0 * 3 + 2] = 0; + recording_transform[1 * 3 + 0] = 0; + recording_transform[1 * 3 + 1] = 2.0 / apple2_screen_height; + recording_transform[1 * 3 + 2] = 0; + recording_transform[2 * 3 + 0] = -1; + recording_transform[2 * 3 + 1] = -1; + recording_transform[2 * 3 + 2] = 1; } tuple window_to_widget(float x, float y) @@ -1686,16 +1854,63 @@ tuple window_to_widget(float x, float y) return make_tuple(wx, wy); } +void save_rgba_to_ppm(const unsigned char *rgba8_pixels, int width, int height, const char *filename) +{ + int row_bytes = width * 4; + + FILE *fp = fopen(filename, "w"); + fprintf(fp, "P6 %d %d 255\n", width, height); + for(int row = 0; row < height; row++) { + for(int col = 0; col < width; col++) { + fwrite(rgba8_pixels + row_bytes * row + col * 4, 1, 3, fp); + } + } + fclose(fp); +} + +void add_rendertarget_to_gif(double now, render_target *rt) +{ + static unsigned char image_recorded[apple2_screen_width * recording_scale * apple2_screen_height * recording_scale * 4]; + + rt->start_rendering(); + + glViewport(0, 0, apple2_screen_width * recording_scale, apple2_screen_height * recording_scale); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + screen_only->draw(now, recording_transform, 0, 0, apple2_screen_width, apple2_screen_height); + + rt->stop_rendering(); + + rt->start_reading(); + + glReadPixels(0, 0, apple2_screen_width * recording_scale, apple2_screen_height * recording_scale, GL_RGBA, GL_UNSIGNED_BYTE, image_recorded); + + // Enable to debug framebuffer operations by writing result to screen.ppm. + if(false) { + save_rgba_to_ppm(image_recorded, apple2_screen_width * recording_scale, apple2_screen_height * recording_scale, "screen.ppm"); + } + + GifWriteFrame(&gif_writer, image_recorded, apple2_screen_width * recording_scale, apple2_screen_height * recording_scale, recording_frame_duration_hundredths, 8, false); + + rt->stop_reading(); +} + static void redraw(GLFWwindow *window) { chrono::time_point now = std::chrono::system_clock::now(); chrono::duration elapsed = now - start_time; + int fbw, fbh; + glfwGetFramebufferSize(window, &fbw, &fbh); + glViewport(0, 0, fbw, fbh); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ui->draw(elapsed.count(), to_screen_transform, 0, 0, gWindowWidth / pixel_to_ui_scale, gWindowHeight / pixel_to_ui_scale); CheckOpenGL(__FILE__, __LINE__); + + if(gif_recording) { + add_rendertarget_to_gif(elapsed.count(), rendertarget_for_recording); + } } static void error_callback(int error, const char* description) @@ -1724,6 +1939,11 @@ static void key(GLFWwindow *window, int key, int scancode, int action, int mods) const char* text = glfwGetClipboardString(window); if (text) event_queue.push_back({PASTE, 0, strdup(text)}); + } else if(super_down && key == GLFW_KEY_R) { + if (action == GLFW_PRESS) { + // Toggle UI, which calls the callbacks. + record_toggle->set_value(!record_toggle->on); + } } else { if(key == GLFW_KEY_CAPS_LOCK) { force_caps_on = true; @@ -2041,7 +2261,6 @@ void iterate(const ModeHistory& history, unsigned long long current_byte) use_joystick = false; } - glfwPollEvents(); } diff --git a/interface.h b/interface.h index 68fbf3b..bd5b9d2 100644 --- a/interface.h +++ b/interface.h @@ -3,7 +3,13 @@ namespace APPLE2Einterface { -enum EventType {NONE, KEYDOWN, KEYUP, RESET, REBOOT, PASTE, SPEED, QUIT, PAUSE, EJECT_FLOPPY, INSERT_FLOPPY}; + +enum EventType +{ + NONE, KEYDOWN, KEYUP, RESET, REBOOT, PASTE, SPEED, QUIT, PAUSE, EJECT_FLOPPY, INSERT_FLOPPY, + REQUEST_ITERATION_PERIOD_IN_MILLIS, /* request fixed simulation time period between calls to iterate() */ + WITHDRAW_ITERATION_PERIOD_REQUEST, /* withdraw request for fixed simulation time */ +}; const int LEFT_SHIFT = 340; const int LEFT_CONTROL = 341;