Digital Oscilloscope GUI base on SDL.

來源:互聯網
上載者:User

Digital Oscilloscope GUI base on SDL.

1.soure code:

 

 /* Bring up a window and play with it */</p><p>#include <stdlib.h><br />#include <stdio.h><br />#include <string.h><br />#include <math.h></p><p>#include "SDL.h"<br />uint32_t wavetab[64];</p><p>void DrawHLine(SDL_Surface *screen, uint32_t x, uint32_t y, uint32_t w,<br />uint32_t color, uint32_t flag)<br />{<br />uint8_t *pixel;<br />uint32_t i;<br />if ((x + w) > screen->w) {<br />w = screen->w - x;<br />}</p><p>pixel = screen->pixels + y * screen->pitch +<br />x * screen->format->BytesPerPixel;</p><p>if (flag == 0)<br />for (i = 0; i < w ; i ++) {<br />*(uint32_t *)pixel = color;<br />pixel += 4;<br />}<br />else<br />for (i = 0; i < w/2; i ++) {<br />*(uint32_t *)pixel = color;<br />pixel += 8;<br />}<br />}</p><p>void DrawVLine(SDL_Surface *screen, uint32_t x, uint32_t y, uint32_t h,<br />uint32_t color, uint32_t flag)<br />{<br />uint8_t *pixel;<br />uint32_t i;<br />if ((y + h) > screen->h) {<br />h = screen->h - x;<br />}</p><p>pixel = screen->pixels + y * screen->pitch +<br />x * screen->format->BytesPerPixel;</p><p>if (flag == 0)<br />for (i = 0; i < h ; i ++) {<br />*(uint32_t *)pixel = color;<br />pixel += screen->pitch;<br />}<br />else<br />for (i = 0; i < h/2 ; i ++) {<br />*(uint32_t *)pixel = color;<br />pixel += (screen->pitch) * 2;<br />}<br />}</p><p>void Drawpoint(SDL_Surface *screen, uint32_t x, uint32_t y, uint32_t color)<br />{<br />uint8_t *pixel;<br />uint32_t i;<br />if (y >= (screen->h - 3)) {<br />return;<br />}</p><p>pixel = screen->pixels + y * screen->pitch +<br />x * screen->format->BytesPerPixel;</p><p>*(uint32_t *)pixel = color;<br />*(uint32_t *)(pixel + screen->pitch) = color;<br />*(uint32_t *)(pixel + 2 * screen->pitch) = color;<br />*(uint32_t *)(pixel + 3 * screen->pitch) = color;<br />}</p><p>void generatewave(uint32_t position)<br />{<br />int i = 0;<br />for(i = 0;i < 64; i++) {<br />wavetab[i] = (sin(2 * 3.1415 * i / 64.0 + position * 3.1415 / 128) + 1) * 25.0;<br />}<br />}</p><p>void DrawBg(SDL_Surface *screen)<br />{<br />SDL_Surface *picture, *displayfmt;<br />char *bmpfile = "logo.bmp";<br />uint32_t color;<br />SDL_Rect rect;</p><p>picture = SDL_LoadBMP(bmpfile);<br />if (picture == NULL) {<br />printf("Load logo.bmp failed!/n");<br />return;<br />}<br />displayfmt = SDL_DisplayFormat(picture);<br />if ( displayfmt == NULL ) {<br />fprintf(stderr, "Couldn't convert image: %s/n", SDL_GetError());<br />return;<br />}</p><p>SDL_FreeSurface(picture);<br />picture = displayfmt;</p><p>color = SDL_MapRGB(screen->format, 0x00, 0x00, 0xFF);<br />rect.x = 0;<br />rect.y = 0;<br />rect.w = screen->w;<br />rect.h = screen->h;<br />SDL_FillRect(screen, &rect, color);</p><p>if ( SDL_BlitSurface(picture, NULL, screen, NULL) < 0 ) { /* print Oscilloscope logo*/<br />fprintf(stderr, "Blit failed: %s/n", SDL_GetError());<br />return;<br />}<br />}</p><p>void CreateWindow(SDL_Surface **window, uint32_t width, uint32_t height)<br />{<br />*window = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,<br />0xFF << 24, 0xff << 16, 0xFF << 8, 0xFF);<br />if (*window == NULL) {<br />printf("Create window failed!/n");<br />return;<br />} else {<br />printf("Create window successfully!/n");<br />}<br />}</p><p>void DrawGrid(SDL_Surface *window)<br />{<br />uint32_t i = 0;<br />uint32_t color;<br />SDL_Rect rect;</p><p>rect.x = 0;<br />rect.y = 0;<br />rect.w = window->w;<br />rect.h = window->h;</p><p>color = SDL_MapRGB(window->format, 0, 0, 0);<br />SDL_FillRect(window, &rect, color);</p><p>color = SDL_MapRGB(window->format, 100, 100, 100);<br />for (i = 0; i < 7; i++) {<br />DrawHLine(window, 0, 40 + i * 40, window->w, color, 1);<br />}</p><p>for (i = 0; i < 11; i++) {<br />DrawVLine(window, 40 + i * 40, 0, window->h, color, 1);<br />}</p><p>for(i =0; i < 39; i++) {<br />DrawHLine(window, 236, 8 + i * 8, 8, color, 0);<br />}</p><p>for(i =0; i < 59; i++) {<br />DrawVLine(window, 8 + i * 8, 156, 8, color, 0);<br />}<br />}</p><p>void DrawWave(SDL_Surface *window)<br />{<br />int i;<br />uint32_t color;<br />color = SDL_MapRGB(window->format, 0xFF,0,0);<br />for(i = 0; i < window->w; i++) {<br />Drawpoint(window, i, window->h/2 - wavetab[i%64], color);<br />}<br />}</p><p>void DrawWave2(SDL_Surface *window)<br />{<br />int i;<br />uint32_t color;<br />color = SDL_MapRGB(window->format, 0xFF,0xff,0);<br />for(i = 0; i < window->w; i++) {<br />Drawpoint(window, i, window->h/2 - (wavetab[(i+15)%64] >> 1), color);<br />}<br />}</p><p>void render(SDL_Surface *src, SDL_Surface *dst)<br />{<br />static uint32_t position = 0;</p><p>position += 10;<br />if (position == 128)<br />position = 0;<br />SDL_Rect update;<br />update.x = 10;<br />update.y = 50;</p><p>DrawGrid(src);<br />generatewave(position);<br />DrawWave(src);<br />DrawWave2(src);</p><p>if ( SDL_BlitSurface(src, NULL, dst, &update) < 0 ) {<br />fprintf(stderr, "Blit failed: %s/n", SDL_GetError());<br />}<br />}</p><p>int main(int argc, char *argv[])<br />{<br /> SDL_Surface *screen, *window;<br /> /* Options */<br /> int w, h;<br /> int desired_bpp;<br /> Uint32 video_flags;<br />uint32_t position = 0;<br /> w = 600;<br /> h = 400;<br /> desired_bpp = 0;<br /> video_flags = 0;</p><p> if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {<br /> fprintf(stderr,<br /> "Couldn't initialize SDL: %s/n", SDL_GetError());<br /> return(1);<br /> } else {<br />printf("SDL_Init is ok!/n");<br />}</p><p>/* Initialize the display */<br /> screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE);<br /> if ( screen == NULL ) {<br /> fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s/n",<br /> w, h, desired_bpp, SDL_GetError());<br /> return -1;<br /> } else {<br />printf("SDL_SetVideoMode is ok!/n");<br />}</p><p>DrawBg(screen);<br />SDL_UpdateRect(screen, 0, 0, 0, 0);<br />CreateWindow(&window, 480, 320);</p><p>uint32_t quit = 0;<br />//int j = 0;<br />while (!quit) {</p><p>render(window, screen);<br />SDL_UpdateRect(screen, 10, 50, 480, 320);</p><p>SDL_Eventevent;<br />uint32_t ret = 0;<br />//j = 0;<br />while (ret = SDL_PollEvent(&event)) {<br />//printf("j = %d,ret = %d/n", j++, ret);<br />switch(event.type) {<br />/*case SDL_MOUSEBUTTONDOWN:<br />break;*/<br />case SDL_KEYDOWN:<br />if (event.key.keysym.sym == SDLK_ESCAPE) {<br />quit = SDL_TRUE;<br />}<br />break;<br />case SDL_QUIT:<br />quit = SDL_TRUE;<br />break;<br />}<br />}<br />SDL_Delay(50);<br />}<br />SDL_Quit();<br />}</p><p>

 

2.X86上運行:

 

 

 

3.How to use SDL APIs:


1).SDL_Init(SDL_INIT_VIDEO) .

Initialize the Video Devices such as Framebuffer ,DireactFB,X11 or OpenGL etc.


2).Set the Video device.

SDL_Surface *screen, *window;

screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE);


3).Draw some graphic data on the backbuffer(SWSurface). 

    uint32_t color;
    SDL_Rect rect;
    color = SDL_MapRGB(screen->format, 0x00, 0x00, 0xFF);  /* Generate the blue color*/
    rect.x = 0;
    rect.y = 0;
    rect.w = screen->w;
    rect.h = screen->h;
    SDL_FillRect(screen, &rect, color);   /* FillRect function */


4).If you want to show some on the screen,please call

SDL_UpdateRect(screen, 30, 30, 480, 320);


5).Create a new RGB surface. 

*window = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
            0xFF << 24, 0xff << 16, 0xFF << 8, 0xFF);


6).BitBlt function(Copy source surface to destination surface).

    if ( SDL_BlitSurface(src, NULL, dst, &update) < 0 ) {
        fprintf(stderr, "Blit failed: %s/n", SDL_GetError());
    }


7).How to get input events.

    while (!quit) { 
        render(window, screen);
        SDL_UpdateRect(screen, 30, 30, 480, 320);    
        SDL_Event    event;
        uint32_t ret = 0;    
        //j = 0;    
        while (ret = SDL_PollEvent(&event)) { 

            /* when get input events,this function return 1,or 0,

                and it's not blocked function */ 
            //printf("j = %d,ret = %d/n", j++, ret);
            switch(event.type) {
                /*case SDL_MOUSEBUTTONDOWN:
                    break;*/
                case SDL_KEYDOWN:
                    if (event.key.keysym.sym == SDLK_ESCAPE) {
                        quit = SDL_TRUE;
                    }
                    break;
                case SDL_QUIT:
                    quit = SDL_TRUE;
                    break;
            }
        }    
        SDL_Delay(50);
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.