我正在浏览SDL 2.0教程,因为自从我学习1.2以来,情况发生了很大变化。我现在有一个非常简单的程序,只是一个窗口类,一个应用程序类和一个纹理类。当我调试和编译我的程序时,我听到这就像我的扬声器发出的尖锐的嗡嗡声。如果我从调试文件夹运行已编译的.exe文件,也会发生这种情况。
我不知道如何解决这个问题。由于它似乎只是我的程序,并且发生在.exe文件中,我怀疑它是视觉工作室的错。虽然我并不是一个真正的程序员,但这只是一个爱好,所以也许就是这样。它对我的程序来说是良性的(到目前为止,因为它很小)但是直接令人讨厌。任何人都知道为什么我的扬声器开始喷出噪音?
提前谢谢。
我将在下面发布我的代码,但我对SDL Audio API没有任何作用。
src.cpp
#include <Windows.h>
#include "KApp.h"
const int winW = 800;
const int winH = 600;
int main(int argc, char** argv ) {
KApp ThisApp;
ThisApp.Run( winW, winH);
return 0;
}
KApp.h
#ifndef __KAPP_H__
#define __KAPP_H__
#include <SDL.h>
#include <vector>
#include "Window.h"
#include "KTexture.h"
class KApp {
private:
enum States {
INIT = 0,
RUNNING,
PAUSED,
HALTED,
};
States GameState;
Window* MainWindow;
std::vector<KTexture*> Textures;
public:
KApp();
~KApp();
void Run(const int &,const int &);
void Handle(SDL_Event* Event);
void Update();
void Render();
void CleanUp();
};
#endif
KApp.cpp
#include "KApp.h"
KApp::KApp() {
GameState = INIT;
MainWindow = NULL;
if(SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
exit(0x1);
}
//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_ALPHA_SIZE, 8);
//SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
//SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
//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_ACCUM_ALPHA_SIZE, 8);
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
return;
}
KApp::~KApp() {
if( MainWindow != NULL ) {
delete MainWindow;
MainWindow = NULL;
}
for( std::vector<KTexture*>::iterator it = Textures.begin(); it != Textures.end(); it++ ) {
delete *it;
}
Textures.clear();
SDL_Quit();
return;
}
void KApp::Handle(SDL_Event* Event) {
if(Event->type == SDL_QUIT) {
GameState = HALTED;
}
}
void KApp::CleanUp() {
}
void KApp::Render() {
MainWindow->Render();
}
void KApp::Run(const int& w,const int& h) {
MainWindow = new Window(w,h,"KApp Main Window");
if(!MainWindow->Create()) {
exit(1);
}
Textures.push_back(new KTexture("E:/Pictures/BKG.bmp"));
Textures[0]->Optimize(MainWindow->Surface());
MainWindow->Blit(Textures[0]->Surface());
GameState = RUNNING;
SDL_Event Event;
while(GameState == RUNNING) {
while(SDL_PollEvent(&Event)) {
Handle(&Event);
}
Update();
Render();
}
CleanUp();
}
void KApp::Update() {}
KWindow.h
#ifndef __WINDOW_H__
#define __WINDOW_H__
#include <SDL.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <string>
class Window {
private:
int Width;
int Height;
std::string Title;
SDL_Window* WNDW;
SDL_Surface* SFC;
public:
Window() : Width(800),Height(600),Title("Window"),WNDW(NULL),SFC(NULL) {}
Window(int w, int h, std::string title) : Width(w),Height(h),Title(title),WNDW(NULL),SFC(NULL) {}
~Window() {
if(WNDW != NULL) {
SDL_DestroyWindow(WNDW);
WNDW = NULL;
}
}
bool Create() {
if( WNDW != NULL ) {
return true;
}
WNDW = SDL_CreateWindow( Title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Width, Height, SDL_WINDOW_SHOWN );
if( WNDW == NULL ) {
return false;
}
SFC = SDL_GetWindowSurface( WNDW );
SDL_FillRect ( SFC , NULL, SDL_MapRGB( SFC->format, 0x00, 0x00, 0x00 ) );
SDL_UpdateWindowSurface( WNDW );
//if((Surface = SDL_SetVideoMode(Width,Height,32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL | SDL_RESIZABLE )) == NULL) {
// return false;
//}
/*
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Width, Height, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
*/
return true;
}
void Clear() {
SDL_FillRect ( SFC , NULL, SDL_MapRGB( SFC->format, 0x00, 0x00, 0x00 ) );
SDL_UpdateWindowSurface( WNDW );
}
void Blit(SDL_Surface* Surf) {
SDL_BlitSurface(Surf, NULL, SFC, NULL);
}
void BlitScaled(SDL_Surface* Surf, int x, int y, int w, int h) {
SDL_Rect MyRectum;
MyRectum.x = x;
MyRectum.y = y;
MyRectum.w = w;
MyRectum.h = h;
SDL_BlitScaled(Surf, NULL, SFC, &MyRectum);
}
void Render() {
SDL_UpdateWindowSurface( WNDW );
}
SDL_Surface* Surface() {
return SFC;
}
};
#endif
KTexture.h
#ifndef __KTEXTURE_H__
#define __KTEXTURE_H__
#include <SDL.h>
#include <string>
class KTexture {
private:
SDL_Surface* Texture;
public:
KTexture() : Texture(NULL) {}
KTexture( std::string path ) : Texture(NULL) {
Load(path);
}
~KTexture() {
if( Texture != NULL ) {
SDL_FreeSurface(Texture);
Texture = NULL;
}
}
bool Load( std::string path ) {
Texture = SDL_LoadBMP( path.c_str() );
if(Texture == NULL) {
return false;
}
return true;
}
void Render() {
Render(0,0);
}
void Render(int x, int y) {
}
SDL_Surface* Surface() {
return Texture;
}
bool Optimize(SDL_Surface* For) {
SDL_Surface* nsurf = SDL_ConvertSurface( Texture, For->format, NULL);
if( nsurf == NULL) {
return false;
}
SDL_FreeSurface(Texture);
Texture = nsurf;
return true;
}
};
#endif
编辑已修复。将此添加到KApp :: Run()函数
Uint32 Time = SDL_GetTicks();
GameState = RUNNING;
SDL_Event Event;
while(GameState == RUNNING) {
while(SDL_PollEvent(&Event)) {
Handle(&Event);
}
Update();
if( SDL_GetTicks() > Time + 300) {
Render();
Time = SDL_GetTicks();
}
else
{
SDL_Delay(1);
}
}
答案 0 :(得分:4)
当你的应用程序以非常高的帧速率运行时,我已经看到了这种情况。高音调通常来自显卡。
尝试在代码中启用vsync或限制应用程序的帧速率。