D
djtraumwelt
Foren As
hallo erstmal.
ich hab mir (wiedermal) einen rauchtimer programmiert. mit SDL, SDL_ttf und SDL_image. das ganze unter windows (bitte nicht schlagen).
funktioniert soweit ganz gut, nur dass eben, während das programm läuft, mein arbeitsspeicher immer voller und voller wird, bis der ganze pc nicht mehr reagiert. wenn ich das programm aus mache, dann dauerts ne weile bis der speicher wieder freigeräumt ist und danach läuft alles wieder. wenn ich mir im taskmanager die timer.exe anschaue, dann kann ich richtig dabei zusehen, wie die MBs immer mehr werden. warum?
wer das ganze mal unter linux compilieren will, der braucht nur die oben genannten sdl bibliotheken macht folgenden code raus:
und compiliert mit:
ich hab mir (wiedermal) einen rauchtimer programmiert. mit SDL, SDL_ttf und SDL_image. das ganze unter windows (bitte nicht schlagen).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
#include <time.h>
int zeit;
double min=1;
int offzeit=0;
SDL_TimerID timer;
Uint32 haupt_funk (Uint32 intervall, void *parameter)
{
if (offzeit == 0) {
zeit--;
}
min += 0.01666666666666666666667;
return intervall;
}
#ifdef main
#undef main
#endif
int main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0 )
{
fprintf(stderr, "SDL konnte nicht initialisiert werden: %s\n",
SDL_GetError());
exit(1);
}
int anzahl=0;
int val=3600;
int quit=0;
int weiter=0;
double qte=0;
int y=50;
int minuten;
FILE *fp;
TTF_Font *font, *sfont;
SDL_Color color = {0,0,0,0};
SDL_Surface *text_to_grafik;
char text[100];
SDL_Event event;
SDL_Surface *display;
SDL_Rect drect, srect;
Uint32 colorkey;
SDL_Surface *image;
image = IMG_Load("zig.jpg");
colorkey = SDL_MapRGB(image->format, 0, 0, 0);
SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
SDL_WM_SetIcon(image,NULL);
display = SDL_SetVideoMode( 800, 200, 0, SDL_SWSURFACE );
zeit = 3600;
timer = SDL_AddTimer (1000, haupt_funk, NULL);
TTF_Init();
font=TTF_OpenFont("segoeprb.ttf",16);
sfont=TTF_OpenFont("segoeprb.ttf",8);
fp=fopen("timer.dat","rb");
if (fread (&minuten, 4, 1, fp) == 1) {
min = (double) (minuten / 60);
fread (&zeit,4,1,fp);
fread (&anzahl,4,1,fp);
fread (&val,4,1,fp);
fread (&y,4,1,fp);
}
fclose(fp);
drect.x=0;
drect.y=50;
drect.w=800;
drect.h=150;
SDL_FillRect(display, &drect, SDL_MapRGB(display->format, 90, 90, 90));
SDL_UpdateRects(display,1,&drect);
quit = 0;
while( quit == 0 )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_q)
{
quit = 1;
break;
}
case SDL_QUIT:
quit = 1;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_RIGHT) {
val = (val - zeit + val) / 2;
zeit = 0;
}
}
}
SDL_Delay(100);
qte = (double) (anzahl / (min / 60));
sprintf(text,"%d in %d noch %d",anzahl,(int) min,zeit);
SDL_WM_SetCaption(text,text);
drect.x=0;
drect.y=0;
drect.w=800;
drect.h=50;
SDL_FillRect(display, &drect, SDL_MapRGB(display->format, 250, 190, 0));
sprintf(text,"Bisher %d Zigaretten in %d Minuten geraucht.",anzahl,(int) min);
text_to_grafik = TTF_RenderText_Solid(font, text, color);
drect.x = 0;
drect.y = 0;
drect.w = 800;
drect.h = 25;
srect.x = 0;
srect.y = 0;
srect.w = 800;
srect.h = 25;
SDL_BlitSurface(text_to_grafik, &srect, display, &drect);
sprintf(text,"Intervall: %d Sekunden (%d min)/ noch: %d Sekunden (%d min).",val,(int) (val / 60),zeit,(int) (zeit / 60));
text_to_grafik = TTF_RenderText_Solid(font, text, color);
drect.x = 0;
drect.y = 25;
drect.w = 800;
drect.h = 25;
srect.x = 0;
srect.y = 0;
srect.w = 800;
srect.h = 25;
SDL_BlitSurface(text_to_grafik, &srect, display, &drect);
drect.x = 0;
drect.y = 0;
drect.w = 800;
drect.h = 50;
SDL_UpdateRects(display,1,&drect);
if (zeit <= 0) {
sprintf(text,"Jetzt rauchen!");
SDL_WM_SetCaption(text,text);
val += 100;
zeit += val;
anzahl++;
offzeit=1;
drect.x=0;
drect.y=0;
drect.w=800;
drect.h=50;
SDL_FillRect(display, &drect, SDL_MapRGB(display->format, 50, 230, 0));
sprintf(text,"Klicken zum rauchen!");
text_to_grafik = TTF_RenderText_Solid(font, text, color);
drect.x = 0;
drect.y = 12;
drect.w = 800;
drect.h = 25;
srect.x = 0;
srect.y = 0;
srect.w = 800;
srect.h = 25;
SDL_BlitSurface(text_to_grafik, &srect, display, &drect);
drect.x = 0;
drect.y = 0;
drect.w = 800;
drect.h = 50;
SDL_UpdateRects(display,1,&drect);
weiter=0;
while(weiter == 0) {
while(SDL_PollEvent(&event)) {
if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {
weiter=1;
offzeit=0;
y += 10;
sprintf(text,"%d. Minute: %d. Zigarette an. Bisherige Qutoe von %f Zigaretten pro Stunde.",(int) min,anzahl,qte);
text_to_grafik = TTF_RenderText_Solid(sfont, text, color);
drect.x = 0;
drect.y = y;
drect.w = 800;
drect.h = 10;
srect.x = 0;
srect.y = 0;
srect.w = 800;
srect.h = 10;
SDL_BlitSurface(text_to_grafik, &srect, display, &drect);
drect.x = 0;
drect.y = y;
drect.w = 800;
drect.h = 10;
SDL_UpdateRects(display,1,&drect);
if (y == 180) {
y=170;
drect.x=0;
drect.y=50;
drect.w=800;
drect.h=140;
srect.x=0;
srect.y=60;
srect.w=800;
srect.h=140;
SDL_BlitSurface(display, &srect, display, &drect);
drect.h=150;
SDL_UpdateRects(display,1,&drect);
}
}
}
}
SDL_Delay(100);
}
}
}
fp=fopen("timer.dat","wb");
minuten = (int) (min * 60);
fwrite (&minuten,4,1,fp);
fwrite (&zeit,4,1,fp);
fwrite (&anzahl,4,1,fp);
fwrite (&val,4,1,fp);
fwrite (&y,4,1,fp);
fclose(fp);
TTF_CloseFont(font);
TTF_CloseFont(sfont);
TTF_Quit();
SDL_RemoveTimer(timer);
SDL_FreeSurface(text_to_grafik);
SDL_Quit();
return 0;
}
funktioniert soweit ganz gut, nur dass eben, während das programm läuft, mein arbeitsspeicher immer voller und voller wird, bis der ganze pc nicht mehr reagiert. wenn ich das programm aus mache, dann dauerts ne weile bis der speicher wieder freigeräumt ist und danach läuft alles wieder. wenn ich mir im taskmanager die timer.exe anschaue, dann kann ich richtig dabei zusehen, wie die MBs immer mehr werden. warum?
wer das ganze mal unter linux compilieren will, der braucht nur die oben genannten sdl bibliotheken macht folgenden code raus:
Code:
#ifdef main
#undef main
#endif
Code:
gcc `sdl-config --cflags --libs` timer.c -o timer -lSDL_ttf -lSDL_image
Zuletzt bearbeitet: