瀏覽代碼

muzyka i może jakieś drobiazgi

mateuszb 18 年之前
父節點
當前提交
de95bb0753
共有 5 個文件被更改,包括 95 次插入2 次删除
  1. 28 1
      CMT.cpp
  2. 65 0
      SDL_Extensions.cpp
  3. 2 0
      SDL_Extensions.h
  4. 二進制
      h3m.txt
  5. 0 1
      stdafx.h

+ 28 - 1
CMT.cpp

@@ -3,7 +3,9 @@
 #include "stdafx.h"
 #include "SDL.h"
 #include "SDL_TTF.h"
+#include "SDL_mixer.h"
 #include "CBuildingHandler.h"
+#include "SDL_Extensions.h"
 #include <cmath>
 #include <stdio.h>
 #include <string.h>
@@ -208,11 +210,36 @@ int _tmain(int argc, _TCHAR* argv[])
 	SDL_Surface *screen, *temp;
 	std::vector<SDL_Surface*> Sprites;
 	float i;
-	if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER/*|SDL_INIT_EVENTTHREAD*/)==0)
+	if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
 	{
 		TTF_Init();
 		atexit(TTF_Quit);
+		atexit(SDL_Quit);
 		TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",16);
+
+		//initializing audio
+		if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048)==-1)
+		{
+			printf("Mix_OpenAudio: %s\n", Mix_GetError());
+			exit(2);
+		}
+		atexit(Mix_CloseAudio);
+		//audio initialized
+
+		Mix_Music *music;
+		music = Mix_LoadMUS("MP3\\MainMenuWoG.mp3");
+		if(!music)
+		{
+			printf("Mix_LoadMUS(\"MainMenuWoG.mp3\"): %s\n", Mix_GetError());
+			// this might be a critical error...
+		}
+
+		/*if(Mix_PlayMusic(music, -1)==-1) //uncomment this fragment to have music
+		{
+			printf("Mix_PlayMusic: %s\n", Mix_GetError());
+			// well, there's no music, but most games don't break without music...
+		}*/
+
 		screen = SDL_SetVideoMode(800,600,24,SDL_HWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
 		ekran = screen;
 		//FILE * zr = fopen("mal.txt","r");

+ 65 - 0
SDL_Extensions.cpp

@@ -149,3 +149,68 @@ SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
 	SDL_FreeSurface(first);
 	return ret;
 }
+ //converts surface to cursor
+SDL_Cursor * CSDL_Ext::SurfaceToCursor(SDL_Surface *image, int hx, int hy)
+{
+	int             w, x, y;
+	Uint8           *data, *mask, *d, *m, r, g, b;
+	Uint32          color;
+	SDL_Cursor      *cursor;
+
+	w = (image->w + 7) / 8;
+	data = (Uint8 *)alloca(w * image->h * 2);
+	if (data == NULL)
+		return NULL;
+	memset(data, 0, w * image->h * 2);
+	mask = data + w * image->h;
+	if (SDL_MUSTLOCK(image))
+		SDL_LockSurface(image);
+	for (y = 0; y < image->h; y++)
+	{
+		d = data + y * w;
+		m = mask + y * w;
+		for (x = 0; x < image->w; x++)
+		{
+			color = CSDL_Ext::SDL_GetPixel(image, x, y);
+			if ((image->flags & SDL_SRCCOLORKEY) == 0 || color != image->format->colorkey)
+			{
+				SDL_GetRGB(color, image->format, &r, &g, &b);
+				color = (r + g + b) / 3;
+				m[x / 8] |= 128 >> (x & 7);
+				if (color < 128)
+					d[x / 8] |= 128 >> (x & 7);
+			}
+		}
+	}
+	if (SDL_MUSTLOCK(image))
+		SDL_UnlockSurface(image);
+	cursor = SDL_CreateCursor(data, mask, w, image->h, hx, hy);
+	return cursor;
+}
+
+Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, int x, int y)
+{
+    int bpp = surface->format->BytesPerPixel;
+    /* Here p is the address to the pixel we want to retrieve */
+    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
+
+    switch(bpp) {
+    case 1:
+        return *p;
+
+    case 2:
+        return *(Uint16 *)p;
+
+    case 3:
+        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
+            return p[0] << 16 | p[1] << 8 | p[2];
+        else
+            return p[0] | p[1] << 8 | p[2] << 16;
+
+    case 4:
+        return *(Uint32 *)p;
+
+    default:
+        return 0;       /* shouldn't happen, but avoids warnings */
+    }
+}

+ 2 - 0
SDL_Extensions.h

@@ -10,6 +10,8 @@ public:
 	static SDL_Surface * hFlip(SDL_Surface * toRot); //horizontal flip
 	static SDL_Surface * rotate02(SDL_Surface * toRot);
 	static SDL_Surface * rotate03(SDL_Surface * toRot);
+	static SDL_Cursor * SurfaceToCursor(SDL_Surface *image, int hx, int hy);
+	static Uint32 SDL_GetPixel(SDL_Surface *surface, int x, int y);
 };
 
 #endif // SDL_EXTENSIONS_H

二進制
h3m.txt


+ 0 - 1
stdafx.h

@@ -12,5 +12,4 @@
 #include <string>
 #include <vector>
 #include <fstream>
-
 // TODO: reference additional headers your program requires here