Eftersom att jag lär mig så får ni gärna klaga på koden också, men ni får också tänka på att allt inte är klart. Kan hända att jag har inkluderat iostream någonstans och glömt att ta bort det också. Vore fint om någon kunde hjälpa mig, har inte en aning om vad jag gör fel
button.h:
Code: Select all
#ifndef BUTTON_H
#define BUTTON_H
#include "image.h"
#include "event.h"
class Button {
public:
Button(const char *file, int x, int y, void (*function)());
void update(SDL_Surface *screen);
private:
void (*clickFunction)();
SDL_Rect box;
bool clicked;
Image image;
Event event;
};
#endif
Code: Select all
#include "button.h"
#include <iostream>
Button::Button(const char *file, int x, int y, void (*function)()) {
clicked = false;
image.load(file);
box.w = image.getWidth();
box.h = image.getHeight();
box.x = x;
box.y = y;
clickFunction = function;
}
void Button::update(SDL_Surface *screen) {
image.show(screen, box.x, box.y);
clicked = false;
event.update();
if (event.current == LEFTDOWN) {
if ((event.getMouseX() > box.x) && (event.getMouseX() < box.x+box.w)
&& (event.getMouseY() > box.y) && (event.getMouseY() < box.y+box.h)) {
clicked = true;
}
}
if (clicked)
clickFunction();
}
Code: Select all
#ifndef IMAGE_H
#define IMAGE_H
#include <SDL_image/SDL_image.h>
#include <iostream>
class Image {
public:
void load(const char *file);
int getWidth() { return image->w; }
int getHeight() { return image->h; }
void show(SDL_Surface *surface, int x, int y);
private:
SDL_Surface *image;
SDL_Rect dest;
};
#endif
Code: Select all
#include "image.h"
void Image::load(const char *file) {
image = IMG_Load(file);
if (!image) {
std::cout << "Error: " << IMG_GetError() << std::endl;
std::exit(1);
}
}
void Image::show(SDL_Surface *surface, int x, int y) {
dest.x = x;
dest.y = y;
SDL_BlitSurface(image, NULL, surface, &dest);
}
Code: Select all
#ifndef EVENT_H
#define EVENT_H
#define LEFTDOWN 1
#define LEFTUP 2
#define RIGHTDOWN 3
#define RIGHTUP 4
#define KEYDOWN 5
#include "SDL.h"
#include <iostream>
class Event {
public:
Event();
void update();
int current;
SDLKey getKey() { return kbInput; }
int getMouseX() { return mousex; }
int getMouseY() { return mousey; }
private:
SDL_Event event;
int mousex, mousey;
SDLKey kbInput;
SDLMod modInput;
Uint16 unicode;
};
#endif
Code: Select all
//den här tycker jag att är lite konstig, men vafan ;D
#include "event.h"
Event::Event() {
current = 0;
kbInput = SDLK_UNKNOWN;
}
void Event::update() {
current = 0;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_MOUSEMOTION:
mousex = event.motion.x;
mousey = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
current = LEFTDOWN;
else if (event.button.button == SDL_BUTTON_RIGHT)
current = RIGHTDOWN;
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
current = LEFTUP;
else if (event.button.button == SDL_BUTTON_RIGHT)
current = RIGHTUP;
break;
case SDL_KEYDOWN:
current = KEYDOWN;
kbInput = event.key.keysym.sym;
modInput = event.key.keysym.mod;
unicode = event.key.keysym.unicode;
break;
case SDL_QUIT:
SDL_Quit();
std::exit(0);
break;
}
}
}