Saturday, October 27, 2012
Real time graph using SDL/C++
#include <iostream>
#include <windows.h>
#include <deque>
#include <SDL/SDL.h>
#include <SDL_draw.h>
#include <cmath>
using namespace std;
MEMORYSTATUS mem();
void Collect_Data(deque<int> *d);
void PutPixel(SDL_Surface* screen, int x, int y);
void DrawPixels(deque<int> *d, SDL_Surface* screen, Uint32 color1);
void DrawLines(SDL_Surface* screen, double x0, double y0, double x1, double y1);
int main(int argc, char* argv[])
{
SDL_Surface* screen = SDL_SetVideoMode(400, 300, 32, SDL_SWSURFACE);
bool running = true;
SDL_Event events;
Uint32 start;
Uint32 color0 = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
Uint32 color1 = SDL_MapRGB(screen->format, 0xff, 0xf0, 0x0f);
deque<int> d;
d.assign(200, -1);
deque<int>::iterator it;
MEMORYSTATUS m = mem();
while(running)
{
start = SDL_GetTicks();
while(SDL_PollEvent(&events))
{
switch(events.type)
{
case SDL_QUIT:
running = false;
break;
}
}
//logic
Collect_Data(&d);
SDL_FillRect(screen, &screen->clip_rect, color0);
DrawPixels(&d, screen, color1);
SDL_Delay(400);
//rendering
SDL_Flip(screen);
//frame regulation
if(1000/30 > SDL_GetTicks() - start)
SDL_Delay(1000/30 - (SDL_GetTicks() - start));
}
SDL_Quit();
return 0;
}
MEMORYSTATUS mem()
{
MEMORYSTATUS m;
m.dwLength = sizeof(m);
return m;
}
void Collect_Data(deque<int> *d)
{
MEMORYSTATUS use = mem();
GlobalMemoryStatus(&use);
if(d->size() < 0)
{
d->push_back(use.dwMemoryLoad);
}
else
{
d->pop_front();
d->push_back(use.dwMemoryLoad);
}
}
void PutPixel(SDL_Surface* screen, int x, int y)
{
Uint32* pexil = (Uint32*) screen->pixels;
Uint32* p = pexil + y*screen->pitch/4 + x;
*p = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
}
void DrawPixels(deque<int> *d, SDL_Surface* screen, Uint32 color1)
{
deque<int>::iterator it;
int i;
for(it = d->begin(), i = 100; it < d->end(); i++, it++)
{
if(*it != -1)
PutPixel(screen, i, 250 -(*it));
}
}
void DrawLines(SDL_Surface* screen, double x0, double y0, double x1, double y1)
{
double x = x1-x0;
double y = y1-y0;
double length = sqrt(x*x + y*y);
double addx = x / length;
double addy = y / length;
x = x0;
y = y0;
for(double i = 0; i < length; i++)
{
PutPixel(screen, (int)x, (int)y);
x+=addx;
y+=addy;
}
}
Subscribe to:
Post Comments (Atom)
How to create Custom push button in QT5
To be honest this is not my work its an exact copy from this link. http://mcmtechtips.blogspot.com/2012/03/shape-qpushbutton-in-your-own-st...
-
Drawing a graph on an x, y plane... you can disregard the load image function, remember i am using the sdl_draw.h library, which is freely a...
-
Progress Bar #include "sdl/sdl.h" #include "sdl/sdl_image.h" #include "SDL/SDL_ttf.h" #include "sdl/...
-
To be honest this is not my work its an exact copy from this link. http://mcmtechtips.blogspot.com/2012/03/shape-qpushbutton-in-your-own-st...
No comments:
Post a Comment