Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <stdio.h>
- #include "Player.h"
- #include <iostream>
- using namespace std;
- const int mainscreen_width = 1920;
- const int mainscreen_height = 1060;
- const int mainscreen_xpoz = 0;
- const int mainscreen_ypoz = 0;
- const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
- Player player1(false, false, false, false, 50, 100, 15, "BMPs/character1");
- Player player2(false, false, false, false, 100, 100, 15, "BMPs/character2");
- SDL_Window* mainwindow = NULL;
- SDL_Surface* mainwindow_surface = NULL;
- SDL_Surface* character = NULL;
- SDL_Surface* character2 = NULL;
- SDL_Surface* backgrounds = NULL;
- SDL_Rect character1movement;
- SDL_Rect character2movement;
- SDL_Event movementevent;
- bool startup();
- bool load();
- bool getinputs();
- void dealwithinputs();
- void resetinputs();
- //void loadbackground(const char* image);
- void endprogram();
- bool startup() {
- bool noerror = true;
- if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
- cout << "error on startup: " << SDL_GetError() << endl;
- noerror = false;
- }
- else {
- mainwindow = SDL_CreateWindow("movementproject v0.5", mainscreen_xpoz, mainscreen_ypoz, mainscreen_width, mainscreen_height, SDL_WINDOW_SHOWN);
- if (mainwindow == NULL) {
- cout << "COULD NOT CREATE WINDOW" << SDL_GetError() << endl;
- noerror = false;
- }
- else {
- mainwindow_surface = SDL_GetWindowSurface(mainwindow);
- }
- return noerror;
- }
- }
- bool load() {
- bool noerror = true;
- character = SDL_LoadBMP("BMPs/character.bmp");
- if (character == NULL) {
- character = SDL_LoadBMP("BMPs/defaultpicture.bmp");
- cout << "CHARACTER PICTURE COULDN'T BE LOADED" << SDL_GetError() << endl;
- noerror = false;
- }
- character2 = SDL_LoadBMP("BMPs/character2.bmp");
- if (character2 == NULL) {
- character2 = SDL_LoadBMP("BMPs/defaultpicture.bmp");
- cout << "CHARACTER2 PICTURE COULDN'T BE LOADED" << SDL_GetError() << endl;
- noerror = false;
- }
- else {
- backgrounds = SDL_LoadBMP("BMPs/background.bmp");
- if (backgrounds == NULL) {
- backgrounds = SDL_LoadBMP("BMPs/defaultpicture.bmp");
- cout << "STARTINGBACKGROUND COULND'T BE LOADED" << SDL_GetError() << endl;
- noerror = false;
- }
- }
- return noerror;
- }
- bool getinputs() {
- bool a_key_is_down = false;
- if (movementevent.type == SDL_KEYDOWN) {
- if (currentKeyStates[SDL_SCANCODE_W])
- {
- player1.ismoving_up = true;
- a_key_is_down = true;
- }
- if (currentKeyStates[SDL_SCANCODE_A]) {
- player1.ismoving_left = true;
- a_key_is_down = true;
- }
- if (currentKeyStates[SDL_SCANCODE_S]) {
- player1.ismoving_down = true;
- a_key_is_down = true;
- }
- if (currentKeyStates[SDL_SCANCODE_D]) {
- player1.ismoving_right = true;
- a_key_is_down = true;
- }
- if (currentKeyStates[SDL_SCANCODE_UP]) {
- player2.ismoving_up = true;
- a_key_is_down = true;
- }
- if (currentKeyStates[SDL_SCANCODE_LEFT]) {
- player2.ismoving_left = true;
- a_key_is_down = true;
- }
- if (currentKeyStates[SDL_SCANCODE_DOWN]) {
- player2.ismoving_down = true;
- a_key_is_down = true;
- }
- if (currentKeyStates[SDL_SCANCODE_RIGHT]) {
- player2.ismoving_right = true;
- a_key_is_down = true;
- }
- }
- cout << a_key_is_down << endl;
- return a_key_is_down;
- }
- void dealwithinputs(){
- if (player1.ismoving_up) {
- character1movement.y -= player1.speed;
- }
- if (player1.ismoving_left) {
- character1movement.x -= player1.speed;
- }
- if (player1.ismoving_down) {
- character1movement.y += player1.speed;
- }
- if (player1.ismoving_right) {
- character1movement.x += player1.speed;
- }
- if (player2.ismoving_up) {
- character2movement.y -= player1.speed;
- }
- if (player2.ismoving_left) {
- character2movement.x -= player1.speed;
- }
- if (player2.ismoving_down) {
- character2movement.y += player1.speed;
- }
- if (player2.ismoving_right) {
- character2movement.x += player1.speed;
- }
- }
- void resetinputs() {
- if (movementevent.type == SDL_KEYUP) {
- if (currentKeyStates[!SDL_SCANCODE_W])
- {
- player1.ismoving_up = false;
- }
- if (currentKeyStates[!SDL_SCANCODE_A]) {
- player1.ismoving_left = false;
- }
- if (currentKeyStates[!SDL_SCANCODE_S]) {
- player1.ismoving_down = false;
- }
- if (currentKeyStates[!SDL_SCANCODE_D]) {
- player1.ismoving_right = false;
- }
- if (currentKeyStates[!SDL_SCANCODE_UP]) {
- player2.ismoving_up = false;
- }
- if (currentKeyStates[!SDL_SCANCODE_LEFT]) {
- player2.ismoving_left = false;
- }
- if (currentKeyStates[!SDL_SCANCODE_DOWN]) {
- player2.ismoving_down = false;
- }
- if (currentKeyStates[!SDL_SCANCODE_RIGHT]) {
- player2.ismoving_right = false;
- }
- }
- }
- /* void loadbackground(const char* image){
- SDL_Surface* backgrounds = SDL_LoadBMP(image);
- if (backgrounds == NULL) {
- cout << "background couldn't be loaded!" << endl;
- }
- } */
- void endprogram() {
- SDL_FreeSurface(mainwindow_surface);
- mainwindow_surface = NULL;
- SDL_FreeSurface(character);
- character = NULL;
- SDL_FreeSurface(backgrounds);
- backgrounds = NULL;
- SDL_DestroyWindow(mainwindow);
- mainwindow = NULL;
- }
- int main(int argc, char* argv[]) {
- startup();
- if (!startup) {
- cout << "STARTUP FAILED" << endl;
- }
- else {
- load();
- if (!load) {
- cout << "LOADING FAILED" << endl;
- }
- else {
- bool running = true;
- SDL_SetColorKey(character, SDL_TRUE, 0xFF00FF);
- while (running) {
- //loadbackground("BMPs/backgound");
- SDL_BlitSurface(backgrounds, NULL, mainwindow_surface, NULL);
- SDL_BlitSurface(character, NULL, mainwindow_surface, &character1movement);
- if (SDL_BlitSurface(character, NULL, mainwindow_surface, &character1movement) != 0) {
- cout << "character couldn't be loaded! " << endl;
- }
- SDL_BlitSurface(character2, NULL, mainwindow_surface, &character2movement);
- if (SDL_BlitSurface(character2, NULL, mainwindow_surface, &character2movement) != 0) {
- cout << "character2 couldn't be loaded! " << endl;
- }
- if (getinputs) {
- getinputs();
- dealwithinputs();
- resetinputs();
- }
- SDL_UpdateWindowSurface(mainwindow);
- }
- }
- }
- endprogram();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement