void CRendering::render(SGlobal* Global) { int i; int iKeyX; int iKeyY = PIANO_Y; int iNote; int iOctave; int iOffset; // background SelectObject(sGDI->hdcmem, (HBRUSH)GetStockObject(WHITE_BRUSH)); Rectangle(sGDI->hdcmem, 0, 0, sGDI->ClientX, sGDI->ClientY); // header drawText(16, 12, sHeader, sGDI->hFontText, RGB(0, 0, 0)); drawText(16, 30, "C++ Music Library", sGDI->hFontText, RGB(0, 0, 0)); drawText(16, 48, "Sample application", sGDI->hFontText, RGB(0, 0, 0)); drawText(16, 86, "Playing! Press P to change the piece. Press ESC to quit...", sGDI->hFontBold, RGB(0, 0, 0)); // first loop: white keys for(i = 0; i < 88; i++) { if(!MCNotes::isNatural(i + LOWEST_NOTE)) continue; iNote = (i + LOWEST_NOTE) % 12; iOctave = (i + LOWEST_NOTE) / 12 - 2; // pitch: [21, 108] --> octave: [0, 7] iOffset = 2 + (iOctave * 7); // initial offset 2: the first C is the 3rd key iKeyY = PIANO_Y + (WHITEKEY_HEIGHT * 4 / 5); switch(iNote) { case D: iOffset += 1; break; case E: iOffset += 2; break; case F: iOffset += 3; break; case G: iOffset += 4; break; case A: iOffset += 5; break; case B: iOffset += 6; break; } iKeyX = 16 + (WHITEKEY_WIDTH / 2) + (WHITEKEY_WIDTH * iOffset); // draw key drawBMP(iKeyX - (WHITEKEY_WIDTH / 2), PIANO_Y, WHITEKEY_WIDTH, WHITEKEY_HEIGHT, sGDI->hdcWhiteKey, sGDI->bmpWhiteKey); if(Global->bKeyPressed[i]) { SelectObject(sGDI->hdcmem, (HBRUSH)sGDI->hKeyBrush[Global->iKeyIntensity[i]]); Ellipse(sGDI->hdcmem, iKeyX - 5, iKeyY - 5, iKeyX + 5, iKeyY + 5); } } // second loop: black keys for(i = 0; i < 88; i++) { if(MCNotes::isNatural(i + LOWEST_NOTE)) continue; iNote = (i + LOWEST_NOTE) % 12; iOctave = (i + LOWEST_NOTE) / 12 - 2; // pitch: [21, 108] --> octave: [0, 7] iOffset = 2 + (iOctave * 7); // initial offset 2: the first C is the 3rd key iKeyY = PIANO_Y + (BLACKKEY_HEIGHT * 2 / 3); switch(iNote) { case Cs: iOffset += 1; break; case Ds: iOffset += 2; break; case Fs: iOffset += 4; break; case Gs: iOffset += 5; break; case As: iOffset += 6; break; } iKeyX = 16 + (WHITEKEY_WIDTH * iOffset); // draw key drawBMP(iKeyX - (WHITEKEY_WIDTH / 2), PIANO_Y, BLACKKEY_WIDTH, BLACKKEY_HEIGHT, sGDI->hdcBlackKey, sGDI->bmpBlackKey); if(Global->bKeyPressed[i]) { SelectObject(sGDI->hdcmem, (HBRUSH)sGDI->hKeyBrush[Global->iKeyIntensity[i]]); Ellipse(sGDI->hdcmem, iKeyX - 5, iKeyY - 5, iKeyX + 5, iKeyY + 5); } } // damper pedal drawBMP((sGDI->WindowSizeX / 2) - (PEDAL_WIDTH / 2), PIANO_Y + WHITEKEY_HEIGHT, PEDAL_WIDTH, PEDAL_HEIGHT, (Global->bDamper)? sGDI->hdcPedalOn: sGDI->hdcPedalOff, (Global->bDamper)? sGDI->bmpPedalOn: sGDI->bmpPedalOff); }
void CRendering::render(SGlobal* Global) { int i; int iKeyX; int iKeyY = PIANO_Y; int iNote; int iOctave; int iOffset; COLORREF cColor; // background SelectObject(sGDI->hdcmem, (HBRUSH)GetStockObject(WHITE_BRUSH)); Rectangle(sGDI->hdcmem, 0, 0, sGDI->ClientX, sGDI->ClientY); // header drawText(16, 12, sHeader, sGDI->hFontText, COLORREF(0x00)); drawText(16, 30, "C++ Music Library", sGDI->hFontText, COLORREF(0x00)); drawText(16, 48, "Sample application", sGDI->hFontText, COLORREF(0x00)); drawText(16, 86, "Playing! Press ESC to quit...", sGDI->hFontBold, COLORREF(0x00)); // hands diagram drawBMP(sGDI->ClientX / 2 - HANDS_WIDTH / 2, 20, HANDS_WIDTH, HANDS_HEIGHT, sGDI->hdcHands, sGDI->bmpHands); // first loop: white keys for(i = 0; i < 88; i++) { if(!MCNotes::isNatural(i + LOWEST_NOTE)) continue; iNote = (i + LOWEST_NOTE) % 12; iOctave = (i + LOWEST_NOTE) / 12 - 2; // pitch: [21, 108] --> octave: [0, 7] iOffset = 2 + (iOctave * 7); // initial offset 2: the first C is the 3rd key iKeyY = PIANO_Y + (WHITEKEY_HEIGHT * 4 / 5); switch(iNote) { case D: iOffset += 1; break; case E: iOffset += 2; break; case F: iOffset += 3; break; case G: iOffset += 4; break; case A: iOffset += 5; break; case B: iOffset += 6; break; } iKeyX = 16 + (WHITEKEY_WIDTH / 2) + (WHITEKEY_WIDTH * iOffset); // draw key drawBMP(iKeyX - (WHITEKEY_WIDTH / 2), PIANO_Y, WHITEKEY_WIDTH, WHITEKEY_HEIGHT, sGDI->hdcWhiteKey, sGDI->bmpWhiteKey); // key pressed if(Global->bKeyPressed[i]) { // left hand: blue if(Global->iKeyHand[i] == 1) cColor = RGB(255 - (Global->iKeyIntensity[i] * 2), 255 - (Global->iKeyIntensity[i] * 2), 255); // right hand: red else cColor = RGB(255, 255 - (Global->iKeyIntensity[i] * 2), 255 - (Global->iKeyIntensity[i] * 2)); // finger number sFinger[0] = Global->iKeyFinger[i] + 48; drawText(iKeyX - 7, iKeyY - 12, sFinger, sGDI->hFontKeys, cColor); } } // second loop: black keys for(i = 0; i < 88; i++) { if(MCNotes::isNatural(i + LOWEST_NOTE)) continue; iNote = (i + LOWEST_NOTE) % 12; iOctave = (i + LOWEST_NOTE) / 12 - 2; // pitch: [21, 108] --> octave: [0, 7] iOffset = 2 + (iOctave * 7); // initial offset 2: the first C is the 3rd key iKeyY = PIANO_Y + (BLACKKEY_HEIGHT * 2 / 3); switch(iNote) { case Cs: iOffset += 1; break; case Ds: iOffset += 2; break; case Fs: iOffset += 4; break; case Gs: iOffset += 5; break; case As: iOffset += 6; break; } iKeyX = 16 + (WHITEKEY_WIDTH * iOffset); // draw key drawBMP(iKeyX - (WHITEKEY_WIDTH / 2), PIANO_Y, BLACKKEY_WIDTH, BLACKKEY_HEIGHT, sGDI->hdcBlackKey, sGDI->bmpBlackKey); // key pressed if(Global->bKeyPressed[i]) { // left hand: blue if(Global->iKeyHand[i] == 1) cColor = RGB(255 - (Global->iKeyIntensity[i] * 2), 255 - (Global->iKeyIntensity[i] * 2), 255); // right hand: red else cColor = RGB(255, 255 - (Global->iKeyIntensity[i] * 2), 255 - (Global->iKeyIntensity[i] * 2)); // finger number sFinger[0] = Global->iKeyFinger[i] + 48; drawText(iKeyX - 7, iKeyY - 12, sFinger, sGDI->hFontKeys, cColor); } } // damper pedal drawBMP((sGDI->WindowSizeX / 2) - (PEDAL_WIDTH / 2), PIANO_Y + WHITEKEY_HEIGHT, PEDAL_WIDTH, PEDAL_HEIGHT, (Global->bDamper)? sGDI->hdcPedalOn: sGDI->hdcPedalOff, (Global->bDamper)? sGDI->bmpPedalOn: sGDI->bmpPedalOff); }
int main(int argc, char **argv) { XEvent evt; Colormap colormap; struct timeval tv; int screenNumber; long eventMask; unsigned long white; unsigned long black; Status rc; int iters; int opt; int draw_lines; int draw_rects; int draw_stipples; int draw_fonts; int draw_image; int zero_counters; int scroll_type; char image_file[256]; char proxy_app[256]; char msg[4096]; // set some defaults g_winWidth = 640; g_winHeight = 480; iters = 5000; draw_lines = 1; draw_rects = 1; draw_stipples = 1; draw_fonts = 1; draw_image = 1; g_delay_dur = 1000; scroll_type = SCROLL_SMOOTH1; zero_counters = 0; strcpy(image_file, "yosemite.bmp"); strcpy(msg, "To be or not to be!"); // process cmd line args opterr = 0; while ((opt = getopt(argc, argv, "lrsg:c:f:i:d:o:z:")) != -1) { switch (opt) { case 'g': if (sscanf(optarg, "%dx%d", &g_winWidth, &g_winHeight) != 2) { fprintf(stderr, "\nerror: invalid geometry specified\n\n"); usage(); return -1; } break; case 'c': if (sscanf(optarg, "%d", &iters) != 1) { fprintf(stderr, "\nerror: invalid count specified\n\n"); usage(); return -1; } break; case 'l': draw_lines = 1; draw_rects = 0; draw_stipples = 0; draw_fonts = 0; draw_image = 0; break; case 'r': draw_rects = 1; draw_lines = 0; draw_stipples = 0; draw_fonts = 0; draw_image = 0; break; case 's': draw_stipples = 1; draw_lines = 0; draw_rects = 0; draw_fonts = 0; draw_image = 0; break; case 'f': if (strlen(optarg) <= 0) { fprintf(stderr, "\nerror: -f option requires an argument\n\n"); usage(); return -1; } draw_fonts = 1; strncpy(msg, optarg, 4096); draw_lines = 0; draw_rects = 0; draw_stipples = 0; draw_image = 0; break; case 'i': if (strlen(optarg) <= 0) { fprintf(stderr, "\nerror: -i option requires an argument\n\n"); usage(); return -1; } draw_image = 1; strncpy(image_file, optarg, 255); draw_lines = 0; draw_rects = 0; draw_stipples = 0; draw_fonts = 0; break; case 'h': usage(); return 0; break; case 'v': printf("xdemo Ver 1.0\n"); return 0; break; case 'd': if (sscanf(optarg, "%d", &g_delay_dur) != 1) { fprintf(stderr, "\nerror: -d option requires an argument\n\n"); usage(); return -1; } break; case 'z': if (strlen(optarg) <= 0) { fprintf(stderr, "\nerror: invalid proxy application specified\n\n"); usage(); return -1; } strcpy(proxy_app, optarg); printf("##### LK_TODO: proxy_app=%s\n", proxy_app); zero_counters = 1; break; case 'o': if (strcmp(optarg, "jump") == 0) { scroll_type = SCROLL_JUMP; } else if (strcmp(optarg, "smooth1") == 0) { scroll_type = SCROLL_SMOOTH1; } else if (strcmp(optarg, "smooth2") == 0) { scroll_type = SCROLL_SMOOTH2; } else if (strcmp(optarg, "smooth3") == 0) { scroll_type = SCROLL_SMOOTH3; } else if (strcmp(optarg, "smooth4") == 0) { scroll_type = SCROLL_SMOOTH4; } else { fprintf(stderr, "\ninvalid scroll type specified\n\n"); usage(); return -1; } break; default: usage(); return -1; } } // must have at least one operation if ((!draw_lines) && (!draw_rects) && (!draw_stipples) && (!draw_fonts) && (!draw_image)) { usage(); return -1; } g_disp = XOpenDisplay(NULL); if (!g_disp) { dprint("error opening X display\n"); exit(-1); } screenNumber = DefaultScreen(g_disp); white = WhitePixel(g_disp, screenNumber); black = BlackPixel(g_disp, screenNumber); g_win = XCreateSimpleWindow(g_disp, DefaultRootWindow(g_disp), 50, 50, // origin g_winWidth, g_winHeight, // size 0, black, // border white ); // backgd XMapWindow(g_disp, g_win); //eventMask = StructureNotifyMask | MapNotify | VisibilityChangeMask; eventMask = StructureNotifyMask | VisibilityChangeMask; XSelectInput(g_disp, g_win, eventMask); g_gc = XCreateGC(g_disp, g_win, 0, // mask of values NULL ); // array of values #if 0 do { dprint("about to call XNextEvent(...)\n"); XNextEvent(g_disp, &evt);// calls XFlush dprint("returned from XNextEvent(...)\n"); } //while(evt.type != MapNotify); while(evt.type != VisibilityNotify); #endif // get access to the screen's color map colormap = DefaultColormap(g_disp, screenNumber); // alloc red color rc = XAllocNamedColor(g_disp, colormap, "red", &g_colors[0], &g_colors[0]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'red' color.\n"); exit(1); } rc = XAllocNamedColor(g_disp, colormap, "green", &g_colors[1], &g_colors[1]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'green' color.\n"); exit(1); } rc = XAllocNamedColor(g_disp, colormap, "blue", &g_colors[2], &g_colors[2]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'blue' color.\n"); exit(1); } rc = XAllocNamedColor(g_disp, colormap, "yellow", &g_colors[3], &g_colors[3]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'yellow' color.\n"); exit(1); } rc = XAllocNamedColor(g_disp, colormap, "orange", &g_colors[4], &g_colors[4]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'orange' color.\n"); exit(1); } if (zero_counters) { signal_tcp_proxy(proxy_app); } if (draw_lines) { start_timer(&tv); drawLines(iters); printf("drew %d lines in %d ms\n", iters, time_elapsed_ms(tv)); } if (draw_rects) { start_timer(&tv); drawRectangles(iters); printf("drew %d rects in %d ms\n", iters, time_elapsed_ms(tv)); } if (draw_stipples) { start_timer(&tv); // LK_TODO } if (draw_fonts) { start_timer(&tv); drawFont(iters, msg); printf("drew %d strings in %d ms\n", iters, time_elapsed_ms(tv)); } if (draw_image) { start_timer(&tv); drawBMP(image_file, scroll_type); printf("drew BMP in %d ms\n", time_elapsed_ms(tv)); } if (zero_counters) { signal_tcp_proxy(proxy_app); } eventMask = ButtonPressMask|ButtonReleaseMask; XSelectInput(g_disp, g_win, eventMask); do { XNextEvent(g_disp, &evt); // calls XFlush() } while(evt.type != ButtonRelease); XDestroyWindow(g_disp, g_win); XCloseDisplay(g_disp); return 0; }