merged changes 718:732 from personal/fatray into trunk

git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@733 6778bc44-b910-0410-a7a0-be141de4315d
This commit is contained in:
fatray
2007-12-10 20:47:00 +00:00
parent cdb35044ce
commit 99d12fa3bd
7 changed files with 293 additions and 391 deletions

View File

@ -1,6 +1,6 @@
PROJECT(projectM-tests)
ADD_EXECUTABLE(projectM-test projectM-test.cpp sdltoprojectM.h video_init.cpp ConfigFile.h ConfigFile.cpp)
ADD_EXECUTABLE(projectM-test projectM-test.cpp sdltoprojectM.h video_init.cpp ConfigFile.h ConfigFile.cpp getConfigFilename.cpp getConfigFilename.h)
ADD_EXECUTABLE(projectM-test-memleak projectM-test-memleak.cpp sdltoprojectM.h video_init.cpp ConfigFile.h ConfigFile.cpp)
ADD_EXECUTABLE(projectM-test-texture projectM-test-texture.cpp sdltoprojectM.h video_init.cpp ConfigFile.h ConfigFile.cpp)

View File

@ -0,0 +1,22 @@
done :
whitespace, unused globals, removed locals that shadowed globals
made fullscreen a bool
refactored eventhandler into a switch case
put read_config into own header with a more descriptive name (getConfigFilename.cpp)
done some include maintenance
Questions:
Does anyone need the global `SDL_Surface screen` ?
Is projectM-test the example projectM implementation?
- create projectM-example
- use projectM-test for testing and debugging
Todo:
projectM-test-texture, necessary?
+ incorporate into projectm-test as an ifdef'd function
projectM-test-memleak, necessary?
+ incorporate into projectm-test as an ifdef'd function
any feedback, please come shout at fatray@users.sourceforge.net,
or leave messages of pure hate on the projectM forums.

View File

@ -0,0 +1,89 @@
//
// File: getConfigFilename.cpp
//
// Author: fatray
//
// Created on 05 December 2007, 23:39
//
// FIXME: portability
// I hacked include<string> on to silence my compiler, is it valid?
#include <string>
#include "getConfigFilename.h"
#include <fcntl.h>
// get the full pathname of a configfile
std::string getConfigFilename()
{
char num[512];
FILE *in;
FILE *out;
char* home;
// FIXME: fixed length buffers are not ideal.
char projectM_home[1024];
char projectM_config[1024];
strcpy(projectM_config, PROJECTM_PREFIX);
strcpy(projectM_config + strlen(PROJECTM_PREFIX), CONFIG_FILE);
projectM_config[strlen(PROJECTM_PREFIX) + strlen(CONFIG_FILE)] = '\0';
printf("dir:%s \n", projectM_config);
home = getenv("HOME");
strcpy(projectM_home, home);
strcpy(projectM_home + strlen(home), "/.projectM/config.inp");
projectM_home[strlen(home) + strlen("/.projectM/config.inp")] = '\0';
if ((in = fopen(projectM_home, "r")))
{
printf("reading ~/.projectM/config.inp \n");
fclose(in);
return std::string(projectM_home);
}
printf("trying to create ~/.projectM/config.inp \n");
projectM_home[strlen(home) + strlen("/.projectM")] = '\0';
mkdir(projectM_home, 0755);
strcpy(projectM_home + strlen(home), "/.projectM/config.inp");
projectM_home[strlen(home) + strlen("/.projectM/config.inp")] = '\0';
if((out = fopen(projectM_home, "w")))
{
if ((in = fopen(projectM_config, "r")))
{
while(fgets(num, 80, in)!=NULL)
{
fputs(num, out);
}
fclose(in);
fclose(out);
if ((in = fopen(projectM_home, "r")))
{
printf("created ~/.projectM/config.inp successfully\n");
fclose(in);
return std::string(projectM_home);
}
printf("This shouldn't happen, using implementation defaults\n");
abort();
}
printf("Cannot find projectM default config, using implementation defaults\n");
abort();
}
printf("Cannot create ~/.projectM/config.inp, using default config file\n");
if ((in = fopen(projectM_config, "r")))
{
printf("Successfully opened default config file\n");
fclose(in);
return std::string(projectM_config);
}
printf("Using implementation defaults, your system is really messed up, I'm suprised we even got this far\n");
abort();
}

View File

@ -0,0 +1,20 @@
//
// File: getConfigFilename.h
//
// Author: fatray
//
// Created on 05 December 2007, 23:39
//
// FIXME: move into getConfigFilename.cpp
// FIXME: portable?
#ifndef _GETCONFIGFILENAME_H
#define _GETCONFIGFILENAME_H
//FIXME: define this here? in .cpp? or somewhere else?
#define CONFIG_FILE "/share/projectM/config.inp"
// get the full pathname of a configfile
std::string getConfigFilename();
#endif /* _GETCONFIGFILENAME_H */

View File

@ -1,6 +1,6 @@
/**
* projectM -- Milkdrop-esque visualisation SDK
* Copyright (C)2003-2004 projectM Team
* Copyright (C)2003-2007 projectM Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -18,243 +18,110 @@
* See 'LICENSE.txt' included within this release
*
*/
#include "video_init.h"
#include <fcntl.h>
#include <stdlib.h>
#include <math.h>
#include <SDL/SDL.h>
#include <libprojectM/projectM.hpp>
#include "sdltoprojectM.h"
#include "ConfigFile.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#define CONFIG_FILE "/share/projectM/config.inp"
std::string read_config();
#ifdef DEBUG
FILE *debugFile = NULL;
#endif
volatile enum {
Init,
Run,
Exit
} client_state = Init;
SDL_Surface *screen;
#include "getConfigFilename.h"
//FIXME: these don't have to be global
projectM *globalPM = NULL;
int wvw, wvh, fvw, fvh;
bool fullscreen;
int dumpFrame = 0;
int frameNumber = 0;
int texsize=512;
int gx=32,gy=24;
int wvw=512,wvh=512;
int fvw=1024,fvh=768;
int fps=30, fullscreen=0;
std::string read_config()
void renderLoop()
{
int n;
char num[512];
FILE *in;
FILE *out;
char* home;
char projectM_home[1024];
char projectM_config[1024];
strcpy(projectM_config, PROJECTM_PREFIX);
strcpy(projectM_config+strlen(PROJECTM_PREFIX), CONFIG_FILE);
projectM_config[strlen(PROJECTM_PREFIX)+strlen(CONFIG_FILE)]='\0';
printf("dir:%s \n",projectM_config);
home=getenv("HOME");
strcpy(projectM_home, home);
strcpy(projectM_home+strlen(home), "/.projectM/config.inp");
projectM_home[strlen(home)+strlen("/.projectM/config.inp")]='\0';
if ((in = fopen(projectM_home, "r")) != 0)
{
printf("reading ~/.projectM/config.inp \n");
fclose(in);
return std::string(projectM_home);
}
else
{
printf("trying to create ~/.projectM/config.inp \n");
strcpy(projectM_home, home);
strcpy(projectM_home+strlen(home), "/.projectM");
projectM_home[strlen(home)+strlen("/.projectM")]='\0';
mkdir(projectM_home,0755);
strcpy(projectM_home, home);
strcpy(projectM_home+strlen(home), "/.projectM/config.inp");
projectM_home[strlen(home)+strlen("/.projectM/config.inp")]='\0';
if((out = fopen(projectM_home,"w"))!=0)
{
if ((in = fopen(projectM_config, "r")) != 0)
{
while(fgets(num,80,in)!=NULL)
{
fputs(num,out);
}
fclose(in);
fclose(out);
if ((in = fopen(projectM_home, "r")) != 0)
{
printf("created ~/.projectM/config.inp successfully\n");
fclose(in);
return std::string(projectM_home);
}
else{printf("This shouldn't happen, using implementation defualts\n");abort();}
}
else{printf("Cannot find projectM default config, using implementation defaults\n");abort();}
}
else
{
printf("Cannot create ~/.projectM/config.inp, using default config file\n");
if ((in = fopen(projectM_config, "r")) != 0)
{ printf("Successfully opened default config file\n");
fclose(in);
return std::string(projectM_config);}
else{ printf("Using implementation defaults, your system is really messed up, I'm suprised we even got this far\n"); abort();}
}
}
abort();
}
void renderLoop() {
int i;
int x, y;
int index;
short pcm_data[2][512];
while ( 1 ) {
projectMEvent evt;
projectMKeycode key;
projectMModifier mod;
/** Process SDL events */
SDL_Event event;
while ( SDL_PollEvent( &event ) ) {
/** Translate into projectM codes and process */
evt = sdl2pmEvent( event );
key = sdl2pmKeycode( event.key.keysym.sym );
mod = sdl2pmModifier( event.key.keysym.mod );
if ( evt == PROJECTM_KEYDOWN ) {
if(key == PROJECTM_K_ESCAPE)
{
delete(globalPM);
exit(0);
while (1)
{
projectMEvent evt;
projectMKeycode key;
projectMModifier mod;
/** Process SDL events */
SDL_Event event;
while (SDL_PollEvent(&event))
{
/** Translate into projectM codes and process */
evt = sdl2pmEvent(event);
key = sdl2pmKeycode(event.key.keysym.sym);
mod = sdl2pmModifier(event.key.keysym.mod);
switch (evt)
{
case PROJECTM_KEYDOWN:
switch (key)
{
case PROJECTM_K_ESCAPE:
delete(globalPM);
exit(0);
break;
case PROJECTM_K_f:
{
fullscreen = !fullscreen;
if (fullscreen)
{
resize_display(fvw, fvh, fullscreen);
globalPM->projectM_resetGL(fvw, fvh);
} else
{
resize_display(wvw, wvh, fullscreen);
globalPM->projectM_resetGL(wvw, wvh);
}
break;
}
case PROJECTM_K_q:
exit(1);
break;
default:
globalPM->key_handler(evt, key, mod);
}
break;
case PROJECTM_VIDEORESIZE:
wvw = event.resize.w;
wvh = event.resize.h;
resize_display(wvw, wvh, 0);
globalPM->projectM_resetGL(wvw, wvh);
break;
default:
// not for us, give it to projectM
globalPM->key_handler(evt, key, mod);
break;
}
}
if(key == SDLK_f)
{
resize_display(fvw, fvh, fullscreen);
globalPM->projectM_resetGL( fvw, fvh );
}
else if(key == SDLK_q) { exit (1);}
else {globalPM->key_handler(evt,key,mod);}
}
else if ( evt == PROJECTM_VIDEORESIZE )
{
wvw=event.resize.w;
wvh=event.resize.h;
resize_display(wvw, wvh, 0);
globalPM->projectM_resetGL( wvw, wvh );
}
}
/** Render the new frame */
globalPM->renderFrame( );
SDL_GL_SwapBuffers();
}
printf("Worker thread: Exiting\n");
}
int main( int argc, char **argv ) {
int i;
char projectM_data[1024];
std::string config_file;
config_file = read_config();
ConfigFile config(config_file);
int wvw = config.read<int>( "Window Width", 512 );
int wvh = config.read<int>( "Window Height", 512 );
int fullscreen = 0;
if (config.read("Fullscreen", true)) fullscreen = 1;
else fullscreen = 0;
#ifdef DEBUG
int value;
int rgb_size[3];
#endif
const SDL_VideoInfo* info = NULL;
int bpp = 0;
/* Flags we will pass into SDL_SetVideoMode. */
int flags = 0;
init_display(wvw,wvh,&fvw,&fvh,fullscreen);
/** Setup some window stuff */
SDL_WM_SetCaption( PROJECTM_TITLE, NULL );
globalPM = new projectM(config_file);
/** Initialise projectM */
/** Initialise the thread */
renderLoop();
return 1;
globalPM->renderFrame();
SDL_GL_SwapBuffers();
}
}
int main(int argc, char **argv)
{
// fix `fullscreen quit kills mouse` issue.
atexit(SDL_Quit);
std::string config_filename = getConfigFilename();
ConfigFile config(config_filename);
// window dimensions from configfile
wvw = config.read<int>("Window Width", 512);
wvh = config.read<int>("Window Height", 512);
fullscreen = config.read("Fullscreen", true);
init_display(wvw, wvh, &fvw, &fvh, fullscreen);
SDL_WM_SetCaption(PROJECTM_TITLE, NULL);
globalPM = new projectM(config_filename);
// if started fullscreen, give PM new viewport dimensions
if (fullscreen)
globalPM->projectM_resetGL(fvw, fvh);
renderLoop();
// not reached
return 1;
}

View File

@ -1,155 +1,62 @@
//video_init.c - SDL/Opengl Windowing Creation/Resizing Functions
//
//by Peter Sperl
//
//Opens an SDL Window and creates an OpenGL session
//also able to handle resizing and fullscreening of windows
//just call init_display again with differant variables
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
extern SDL_Surface *screen;
extern int texsize;
void setup_opengl( int w, int h );
void close_display() {
SDL_Quit();
}
void resize_display(int w, int h, int f) {
int flags;
if (f) flags = SDL_OPENGL|SDL_HWSURFACE|SDL_FULLSCREEN;
else flags = SDL_OPENGL|SDL_HWSURFACE|SDL_RESIZABLE;
// SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
screen = SDL_SetVideoMode( w, h, 0, flags ) ;
if(screen == 0 ) {
fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) );
return;
}
setup_opengl(w,h);
SDL_ShowCursor(f ? SDL_DISABLE : SDL_ENABLE);
}
//init_display
//
//Sets screen to new width and height (w,h)
//Also switches between fullscreen and windowed
//with the boolean f (fullscreen)
void init_display(int w, int h, int *fvw, int *fvh, int f)
{
/* Information about the current video settings. */
const SDL_VideoInfo* info = NULL;
int bpp = 0;
/* Flags we will pass into SDL_SetVideoMode. */
int flags = 0;
/* First, initialize SDL's video subsystem. */
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0 ) {
/* Failed, exit. */
fprintf( stderr, "Video initialization failed: %s\n",
SDL_GetError( ) );
//projectM_vtable.disable_plugin (&projectM_vtable);
return;
}
/* Let's get some video information. */
info = SDL_GetVideoInfo( );
if( !info ) {
/* This should probably never happen. */
fprintf( stderr, "Video query failed: %s\n",
SDL_GetError( ) );
// projectM_vtable.disable_plugin (&projectM_vtable);
return;
}
printf("Screen Resolution: %d x %d\n", info->current_w, info->current_h);
*fvw = info->current_w;
*fvh = info->current_h;
bpp = info->vfmt->BitsPerPixel;
//SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
//SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
//SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
// SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 8 );
// SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 8 );
// SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
if (f==0)
flags = SDL_OPENGL|SDL_HWSURFACE|SDL_RESIZABLE;
else flags = SDL_OPENGL|SDL_HWSURFACE|SDL_FULLSCREEN;
screen= SDL_SetVideoMode( w, h, bpp, flags ) ;
if(screen == 0 ) {
/*
* This could happen for a variety of reasons,
* including DISPLAY not being set, the specified
* resolution not being available, etc.
*/
fprintf( stderr, "Video mode set failed: %s\n",
SDL_GetError( ) );
// projectM_vtable.disable_plugin (&projectM_vtable);
return;
}
// setup_opengl(w,h);
//gluOrtho2D(0, w, 0, h);
}
void setup_opengl( int w, int h )
{
/* Our shading model--Gouraud (smooth). */
glShadeModel( GL_SMOOTH);
/* Culling. */
// glCullFace( GL_BACK );
// glFrontFace( GL_CCW );
// glEnable( GL_CULL_FACE );
/* Set the clear color. */
glClearColor( 0, 0, 0, 0 );
/* Setup our viewport. */
glViewport( 0, 0, w, h );
/*
* Change to the projection matrix and set
* our viewing volume.
*/
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
// gluOrtho2D(0.0, (GLfloat) width, 0.0, (GLfloat) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// glFrustum(0.0, height, 0.0,width,10,40);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGB,0,0,texsize,texsize,0);
//glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,texsize,texsize);
glLineStipple(2, 0xAAAA);
}
//video_init.c - SDL/Opengl Windowing Creation/Resizing Functions
//
//by Peter Sperl & Ray Telford
//
//Opens an SDL Window and creates an OpenGL session
//also able to handle resizing and fullscreening of windows
//just call init_display again with different variables
#include <SDL/SDL.h>
void resize_display(int w, int h, bool f)
{
int flags = SDL_OPENGL|SDL_HWSURFACE|(f ? SDL_FULLSCREEN : SDL_RESIZABLE);
// 0 : use current bits per pixel
if(!SDL_SetVideoMode(w, h, 0, flags))
{
fprintf(stderr, "Video mode set failed: %s\n", SDL_GetError());
return;
}
SDL_ShowCursor(f ? SDL_DISABLE : SDL_ENABLE);
}
//init_display
//
//Sets screen to new width and height (w,h)
//Also switches between fullscreen and windowed
//with the boolean f (fullscreen)
void init_display(int w, int h, int *fvw, int *fvh, bool f)
{
/* First, initialize SDL's video subsystem. */
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
{
fprintf(stderr, "Video initialization failed: %s\n",
SDL_GetError());
return;
}
/* Let's get some video information. */
const SDL_VideoInfo* info = SDL_GetVideoInfo();
if(!info)
{
/* This should probably never happen. */
fprintf(stderr, "Video query failed: %s\n",
SDL_GetError());
// todo:what is this?
// projectM_vtable.disable_plugin (&projectM_vtable);
return;
}
printf("Screen Resolution: %d x %d\n", info->current_w, info->current_h);
*fvw = info->current_w;
*fvh = info->current_h;
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (f)
resize_display(*fvw, *fvh, f);
else
resize_display(w, h, f);
}

View File

@ -1,6 +1,3 @@
void setup_opengl( int w, int h );
void init_display( int w, int h, int *fvw, int *fvh, int fullscreen );
void resize_display( int w, int h, int fullscreen );
void close_display();
void CreateRenderTarget(int texsize,int *RenderTargetTextureID, int *RenderTarget);
void init_display(int w, int h, int *fvw, int *fvh, bool fullscreen);
void resize_display(int w, int h, bool fullscreen);