I know the code is not completely object oriented but that is not what i am focusing on now
And by the way, if you find any other things that i should have i mind please tell me, i wanna learn as much as possible
And another question while i'm anyway starting a thread. Do i really have to pass in the screen surface to the button. Are there no way to make the screen variable global somehow. Seems dumb to do it this way.
I have been sitting up half night now only looking at this....
Thanks in advance!
Here is the main.cpp
#include <sdl.h>
#include <string>
#include "NewButton.h"
SDL_Event event;
bool running = true;
SDL_Surface * screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
int main(int argc, char * args[]){
SDL_Surface * Image = SDL_LoadBMP("TestKnapp.BMP");
Image = SDL_DisplayFormat(Image);
NewButton ExitButton(400,300,100,50,Image,screen);
while(running){
if(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){running = false; SDL_Quit(); break;}
if(event.type == SDL_KEYDOWN){
if(event.key.keysym.sym == SDLK_ESCAPE){running = false; SDL_Quit(); break;}
}
if(ExitButton.CheckEvents()){
running = false;
SDL_Quit();
break;
}
}
ExitButton.DrawToScreen();
SDL_Flip(screen);
}
return 0;
}
Here is NewButton.h
#pragma once
#ifndef newbutton
#define newbutton
#include <SDL.h>
#include <string>
using namespace std;
class NewButton
{
public:
NewButton(int x,int y, int w, int h,SDL_Surface * Image,SDL_Surface * screen);
void DrawToScreen();
bool CheckEvents();
private:
SDL_Surface * LocalScreen;
SDL_Surface * ButtonImage;
SDL_Rect ButtonRect;
SDL_Event event;
int MouseX,MouseY;
};
#endifHere is NewButton.cpp
#include "NewButton.h"
#include <SDL.h>
#include <string>
using namespace std;
NewButton::NewButton(int x,int y, int w, int h,SDL_Surface * Image,SDL_Surface * screen):
LocalScreen(screen),
MouseX(0),
MouseY(0)
{
ButtonImage = Image;
ButtonRect.x = x;
ButtonRect.y = y;
ButtonRect.w = w;
ButtonRect.h = h;
}
void NewButton::DrawToScreen(){
SDL_BlitSurface(ButtonImage,NULL,LocalScreen,&ButtonRect);
}
bool NewButton::CheckEvents(){
if(SDL_PollEvent(&event)){
if(event.type == SDL_MOUSEBUTTONDOWN){
if(event.button.button == SDL_BUTTON_LEFT){
MouseX = event.button.x;
MouseY = event.button.y;
if((MouseX > ButtonRect.x) && (MouseY > ButtonRect.y) && (MouseX < (ButtonRect.x + ButtonRect.w)) && (MouseY < (ButtonRect.y + ButtonRect.h))){
return true;
}
}
}
}
return false;
}






