ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello,
I have an SDL surface representing the audio of a song.
I would like to show on the screen the current portion that is played
as a cursor on the middle and scroll it while music goes on.
I think some openGL function could help me.
Suggestions?
Thanks,
tano
The effect you are talking about is referred to as scrolling see this page for information about it. Using OpenGL you want to adjust the GL_MODELVIEW matrix so that the correct portion of the background is in the buffer.
Hello,
I have an SDL surface representing the audio of a song.
I would like to show on the screen the current portion that is played
as a cursor on the middle and scroll it while music goes on.
I think some openGL function could help me.
Suggestions?
Thanks,
tano
It's very easy to do.
Either build surface for entire song and scroll it, or draw surface only for currently visible segment.
Code:
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <wchar.h>
#include <math.h>
const double pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854874462379962749567351885752724891227938183011949129833673362; //:-)
static int scrWidth = 640;
static int scrHeight = 480;
float song(float pos){
float result = 0.0f;
const int numHarmonics = 5;
for (int i = 0; i < numHarmonics; i++){
float f = (1 << i);
result += sinf(pi*pos*f);
}
result /= (float)numHarmonics;
return result;
//return sinf(pi*pos) + sinf(pi*;
}
int main(int argc, char** argv){
SDL_Init(SDL_INIT_VIDEO);
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_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){
fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480);
SDL_Quit();
return -1;
}
SDL_ShowCursor(SDL_DISABLE);
glClearColor(0.2, 0.2, 0.2, 0.2);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
bool running = true;
Uint32 lastTick = SDL_GetTicks();
Uint32 curTick = lastTick;
while (running){
SDL_Event event;
if (SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running = false;
break;
};
}
curTick = SDL_GetTicks();
float delta = float(curTick - lastTick)/1000.0f;
lastTick = curTick;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, scrWidth, scrHeight/2.0f, -scrHeight/2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
static float offset = 0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
glColor3f(0, 1, 0);
offset += delta;
if (offset > 3.0f)
offset -= 3.0f;
float lineOffset = offset - 0.5f;
float cursorPos = 0.5f;
if (lineOffset < 0){
cursorPos = lineOffset+0.5;
lineOffset = 0;
}
if (lineOffset > 2.0f){
cursorPos = lineOffset - 1.5f;
lineOffset = 2.0f;
}
for (int x = 0; x < scrWidth; x ++)
glVertex2f(x, scrHeight*song(((float)x/(float)scrWidth)+lineOffset)*0.5f);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2f(cursorPos*scrWidth, scrHeight*0.5f);
glVertex2f(cursorPos*scrWidth, -scrHeight*0.5f);
glEnd();
glDisable(GL_DEPTH_TEST);
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
With surface it'll be same thing.
--EDIT--
By the way, you can't render SDL_surface with OpenGL. Either make OpenGL texture from that surface and render it, or don't use OpenGL and use SDL_BlitSurface function.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.